lib

Exception.h
1//---------------------------------------------------------------------------//
2// Copyright 1998 The Regents of the University of California.
3// All rights reserved. See LEGAL.LLNL for full text and disclaimer.
4//---------------------------------------------------------------------------//
5
6#ifndef __CXX_Exception_h
7#define __CXX_Exception_h
8
9#include "Python.h"
10#include "Config.h"
11#include "IndirectPythonInterface.h"
12
13#include <string>
14#include <iostream>
15
16// This mimics the Python structure, in order to minimize confusion
17namespace Py
18 {
19 class ExtensionExceptionType;
20
21 class Exception
22 {
23 public:
24 Exception( ExtensionExceptionType &exception, const std::string& reason );
25
26 explicit Exception ()
27 {}
28
29 Exception (const std::string& reason)
30 {
31 PyErr_SetString (Py::_Exc_RuntimeError(), reason.c_str());
32 }
33
34 Exception (PyObject* exception, const std::string& reason)
35 {
36 PyErr_SetString (exception, reason.c_str());
37 }
38
39
40 void clear() // clear the error
41 // technically but not philosophically const
42 {
43 PyErr_Clear();
44 }
45 };
46
47
48 // Abstract
49 class StandardError: public Exception
50 {
51 protected:
52 explicit StandardError()
53 {}
54 };
55
56 class LookupError: public StandardError
57 {
58 protected:
59 explicit LookupError()
60 {}
61 };
62
63 class ArithmeticError: public StandardError
64 {
65 protected:
66 explicit ArithmeticError()
67 {}
68 };
69
70 class EnvironmentError: public StandardError
71 {
72 protected:
73 explicit EnvironmentError()
74 {}
75 };
76
77 // Concrete
78
79 class TypeError: public StandardError
80 {
81 public:
82 TypeError (const std::string& reason)
83 : StandardError()
84 {
85 PyErr_SetString (Py::_Exc_TypeError(),reason.c_str());
86 }
87 };
88
89 class IndexError: public LookupError
90 {
91 public:
92 IndexError (const std::string& reason)
93 : LookupError()
94 {
95 PyErr_SetString (Py::_Exc_IndexError(), reason.c_str());
96 }
97 };
98
99 class AttributeError: public StandardError
100 {
101 public:
102 AttributeError (const std::string& reason)
103 : StandardError()
104 {
105 PyErr_SetString (Py::_Exc_AttributeError(), reason.c_str());
106 }
107 };
108
109 class NameError: public StandardError
110 {
111 public:
112 NameError (const std::string& reason)
113 : StandardError()
114 {
115 PyErr_SetString (Py::_Exc_NameError(), reason.c_str());
116 }
117 };
118
119 class RuntimeError: public StandardError
120 {
121 public:
122 RuntimeError (const std::string& reason)
123 : StandardError()
124 {
125 PyErr_SetString (Py::_Exc_RuntimeError(), reason.c_str());
126 }
127 };
128
129 class SystemError: public StandardError
130 {
131 public:
132 SystemError (const std::string& reason)
133 : StandardError()
134 {
135 PyErr_SetString (Py::_Exc_SystemError(),reason.c_str());
136 }
137 };
138
139 class KeyError: public LookupError
140 {
141 public:
142 KeyError (const std::string& reason)
143 : LookupError()
144 {
145 PyErr_SetString (Py::_Exc_KeyError(),reason.c_str());
146 }
147 };
148
149
150 class ValueError: public StandardError
151 {
152 public:
153 ValueError (const std::string& reason)
154 : StandardError()
155 {
156 PyErr_SetString (Py::_Exc_ValueError(), reason.c_str());
157 }
158 };
159
160 class OverflowError: public ArithmeticError
161 {
162 public:
163 OverflowError (const std::string& reason)
164 : ArithmeticError()
165 {
166 PyErr_SetString (Py::_Exc_OverflowError(), reason.c_str());
167 }
168 };
169
170 class ZeroDivisionError: public ArithmeticError
171 {
172 public:
173 ZeroDivisionError (const std::string& reason)
174 : ArithmeticError()
175 {
176 PyErr_SetString (Py::_Exc_ZeroDivisionError(), reason.c_str());
177 }
178 };
179
180 class FloatingPointError: public ArithmeticError
181 {
182 public:
183 FloatingPointError (const std::string& reason)
184 : ArithmeticError()
185 {
186 PyErr_SetString (Py::_Exc_FloatingPointError(), reason.c_str());
187 }
188 };
189
190 class MemoryError: public StandardError
191 {
192 public:
193 MemoryError (const std::string& reason)
194 : StandardError()
195 {
196 PyErr_SetString (Py::_Exc_MemoryError(), reason.c_str());
197 }
198 };
199
200 class SystemExit: public StandardError
201 {
202 public:
203 SystemExit (const std::string& reason)
204 : StandardError()
205 {
206 PyErr_SetString (Py::_Exc_SystemExit(),reason.c_str());
207 }
208 };
209
210 }// Py
211
212#endif