Python2/Exception.hxx

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //
00003 // Copyright (c) 1998 - 2007, The Regents of the University of California
00004 // Produced at the Lawrence Livermore National Laboratory
00005 // All rights reserved.
00006 //
00007 // This file is part of PyCXX. For details,see http://cxx.sourceforge.net/. The
00008 // full copyright notice is contained in the file COPYRIGHT located at the root
00009 // of the PyCXX distribution.
00010 //
00011 // Redistribution  and  use  in  source  and  binary  forms,  with  or  without
00012 // modification, are permitted provided that the following conditions are met:
00013 //
00014 //  - Redistributions of  source code must  retain the above  copyright notice,
00015 //    this list of conditions and the disclaimer below.
00016 //  - Redistributions in binary form must reproduce the above copyright notice,
00017 //    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
00018 //    documentation and/or materials provided with the distribution.
00019 //  - Neither the name of the UC/LLNL nor  the names of its contributors may be
00020 //    used to  endorse or  promote products derived from  this software without
00021 //    specific prior written permission.
00022 //
00023 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
00024 // AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
00025 // IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
00026 // ARE  DISCLAIMED.  IN  NO  EVENT  SHALL  THE  REGENTS  OF  THE  UNIVERSITY OF
00027 // CALIFORNIA, THE U.S.  DEPARTMENT  OF  ENERGY OR CONTRIBUTORS BE  LIABLE  FOR
00028 // ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR CONSEQUENTIAL
00029 // DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
00030 // SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
00031 // CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
00032 // LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
00033 // OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00034 // DAMAGE.
00035 //
00036 //-----------------------------------------------------------------------------
00037 
00038 #ifndef __CXX_Exception_h
00039 #define __CXX_Exception_h
00040 
00041 #include "CXX/WrapPython.h"
00042 #include "CXX/Version.hxx"
00043 #include "CXX/Config.hxx"
00044 #include "CXX/IndirectPythonInterface.hxx"
00045 
00046 #include <string>
00047 #include <iostream>
00048 
00049 // This mimics the Python structure, in order to minimize confusion
00050 namespace Py
00051 {
00052     class ExtensionExceptionType;
00053 
00054     class Object;
00055 
00056     class PYCXX_EXPORT Exception
00057     {
00058     public:
00059         Exception( ExtensionExceptionType &exception, const std::string& reason );
00060         Exception( ExtensionExceptionType &exception, Object &reason );
00061 
00062         explicit Exception ()
00063         {}
00064         
00065         Exception (const std::string& reason)
00066         {
00067             PyErr_SetString (Py::_Exc_RuntimeError(), reason.c_str());
00068         }
00069         
00070         Exception (PyObject* exception, const std::string& reason)
00071         {
00072             PyErr_SetString (exception, reason.c_str());
00073         }
00074         
00075         Exception (PyObject* exception, Object &reason);        
00076 
00077         void clear() // clear the error
00078         // technically but not philosophically const
00079         {
00080             PyErr_Clear();
00081         }
00082     };
00083     
00084     
00085     // Abstract
00086     class PYCXX_EXPORT StandardError: public Exception
00087     {
00088     protected: 
00089         explicit StandardError()
00090         {}
00091     };
00092     
00093     class PYCXX_EXPORT LookupError: public StandardError
00094     {
00095     protected: 
00096         explicit LookupError()
00097         {}
00098     };
00099     
00100     class PYCXX_EXPORT ArithmeticError: public StandardError
00101     {
00102     protected: 
00103         explicit ArithmeticError()
00104         {}
00105     };
00106     
00107     class PYCXX_EXPORT EnvironmentError: public StandardError
00108     {
00109     protected: 
00110         explicit EnvironmentError()
00111         {}
00112     };
00113     
00114     // Concrete
00115     
00116     class PYCXX_EXPORT TypeError: public StandardError
00117     {
00118     public:
00119         TypeError (const std::string& reason)
00120             : StandardError()
00121         {
00122             PyErr_SetString (Py::_Exc_TypeError(),reason.c_str());
00123         }
00124     };
00125     
00126     class PYCXX_EXPORT IndexError: public LookupError
00127     {
00128     public:
00129         IndexError (const std::string& reason)
00130             : LookupError()
00131         {
00132             PyErr_SetString (Py::_Exc_IndexError(), reason.c_str());
00133         }
00134     };
00135     
00136     class PYCXX_EXPORT AttributeError: public StandardError
00137     {
00138     public:
00139         AttributeError (const std::string& reason)
00140             : StandardError()
00141         {
00142             PyErr_SetString (Py::_Exc_AttributeError(), reason.c_str());
00143         }        
00144     };
00145     
00146     class PYCXX_EXPORT NameError: public StandardError
00147     {
00148     public:
00149         NameError (const std::string& reason)
00150             : StandardError()
00151         {
00152             PyErr_SetString (Py::_Exc_NameError(), reason.c_str());
00153         }
00154     };
00155     
00156     class PYCXX_EXPORT RuntimeError: public StandardError
00157     {
00158     public:
00159         RuntimeError (const std::string& reason)
00160             : StandardError()
00161         {
00162             PyErr_SetString (Py::_Exc_RuntimeError(), reason.c_str());
00163         }
00164     };
00165     
00166     class PYCXX_EXPORT SystemError: public StandardError
00167     {
00168     public:
00169         SystemError (const std::string& reason)
00170             : StandardError()
00171         {
00172             PyErr_SetString (Py::_Exc_SystemError(),reason.c_str());
00173         }
00174     };
00175     
00176     class PYCXX_EXPORT KeyError: public LookupError
00177     {
00178     public:
00179         KeyError (const std::string& reason)
00180             : LookupError()
00181         {
00182             PyErr_SetString (Py::_Exc_KeyError(),reason.c_str());
00183         }
00184     };
00185     
00186     
00187     class PYCXX_EXPORT ValueError: public StandardError
00188     {
00189     public:
00190         ValueError (const std::string& reason)
00191             : StandardError()
00192         {
00193             PyErr_SetString (Py::_Exc_ValueError(), reason.c_str());
00194         }
00195     };
00196     
00197     class PYCXX_EXPORT OverflowError: public ArithmeticError
00198     {
00199     public:
00200         OverflowError (const std::string& reason)
00201             : ArithmeticError()
00202         {
00203             PyErr_SetString (Py::_Exc_OverflowError(), reason.c_str());
00204         }        
00205     };
00206     
00207     class PYCXX_EXPORT ZeroDivisionError: public ArithmeticError
00208     {
00209     public:
00210         ZeroDivisionError (const std::string& reason)
00211             : ArithmeticError() 
00212         {
00213             PyErr_SetString (Py::_Exc_ZeroDivisionError(), reason.c_str());
00214         }
00215     };
00216     
00217     class PYCXX_EXPORT FloatingPointError: public ArithmeticError
00218     {
00219     public:
00220         FloatingPointError (const std::string& reason)
00221             : ArithmeticError() 
00222         {
00223             PyErr_SetString (Py::_Exc_FloatingPointError(), reason.c_str());
00224         }
00225     };
00226     
00227     class PYCXX_EXPORT MemoryError: public StandardError
00228     {
00229     public:
00230         MemoryError (const std::string& reason)
00231             : StandardError()
00232         {
00233             PyErr_SetString (Py::_Exc_MemoryError(), reason.c_str());
00234         }    
00235     };
00236     
00237     class PYCXX_EXPORT SystemExit: public StandardError
00238     {
00239     public:
00240         SystemExit (const std::string& reason)
00241             : StandardError() 
00242         {
00243             PyErr_SetString (Py::_Exc_SystemExit(),reason.c_str());
00244         }
00245     };
00246 
00247 }// Py
00248 
00249 #endif

Generated on Wed Nov 23 19:00:10 2011 for FreeCAD by  doxygen 1.6.1