EMAN2
exception.h
Go to the documentation of this file.
1/*
2 * Author: Liwei Peng, 07/12/2004 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef eman__exception_h__
33#define eman__exception_h__ 1
34
35#include <log.h>
36
37#include <string>
38
39using std::string;
40
41#include <sstream>
42using std::stringstream;
43
63namespace EMAN {
64
67 class E2Exception : public std::exception {
68 public:
77 explicit E2Exception(const string& file = "", int line = 0,
78 const string& desc_str = "", const string& objname_str = "")
79 : filename(file), linenum(line), desc(desc_str), objname(objname_str)
80 {
81 // Log the message
82 stringstream ss;
83 string line_str;
84 ss << line;
85 ss >> line_str;
86// We shouldn't log all exceptions, since they are frequently used intentionally, with no implied error
87// string message = "File " + file + " line " + line_str + " description " + desc_str + " " + objname_str;
88// LOGERR( message.c_str() );
89 }
90
91 virtual ~E2Exception() throw() {}
92
98 virtual const char *what() const throw();
99
103 virtual const char *name() const { return "Exception"; }
104 protected:
105 string filename;
107 string desc;
108 string objname;
109 };
110
111
119 {
120 public:
121 _NotExistingObjectException(const string& objname_str,
122 const string& file = "unknown",
123 int line = 0,
124 const string& desc_str = "none")
125 : E2Exception(file, line, desc_str, objname_str) {}
126
127 const char *name() const { return "NotExistingObjectException"; }
128
129 };
130#define NotExistingObjectException(objname, desc) \
131 _NotExistingObjectException(objname, __FILE__, __LINE__, desc)
132
133
137 {
138 public:
139 _ImageFormatException(const string& desc_str,
140 const string& file = "unknown",
141 int line = 0)
142 : E2Exception(file, line, desc_str) {}
143
144 const char *name() const { return "ImageFormatException"; }
145
146 };
147#define ImageFormatException(desc) _ImageFormatException(desc, __FILE__, __LINE__)
148
149
157 {
158 public:
159 _ImageDimensionException(const string& desc_str, const string& file = "unknown",
160 int line = 0)
161 : E2Exception(file, line, desc_str) {}
162
163 const char *name() const { return "ImageDimensionException"; }
164
165 };
166#define ImageDimensionException(desc) _ImageDimensionException(desc, __FILE__, __LINE__)
167
168
175 {
176 public:
177 _FileAccessException(const string& filename_str, const string& file = "unknown",
178 int line = 0, const string& desc_str = "")
179 : E2Exception(file, line, desc_str, filename_str)
180 {
181 desc = "cannot access file '" + filename_str + "'";
182 }
183
184 const char *name() const { return "FileAccessException"; }
185
186 };
187#define FileAccessException(filename) _FileAccessException(filename, __FILE__, __LINE__)
188
195 {
196 public:
197 _ImageReadException(const string& imagename, const string& file = "unknown",
198 int line = 0, const string& desc_str = "")
199 : E2Exception(file, line, desc_str, imagename) {}
200
201 const char *name() const { return "ImageReadException"; }
202
203 };
204#define ImageReadException(filename, desc) \
205 _ImageReadException(filename, __FILE__, __LINE__, desc)
206
207
214 {
215 public:
216 _ImageWriteException(const string& imagename, const string& file = "unknown",
217 int line = 0, const string& desc_str = "")
218 : E2Exception(file, line, desc_str, imagename) {}
219
220 const char *name() const { return "ImageWriteException"; }
221
222 };
223#define ImageWriteException(imagename, desc) \
224 _ImageWriteException(imagename, __FILE__, __LINE__, desc)
225
226
232 {
233 public:
234 _NullPointerException(const string& file = "unknown",
235 int line = 0, const string& desc_str = "")
236 : E2Exception(file, line, desc_str) {}
237
238 const char *name() const { return "NullPointerException"; }
239
240 };
241#define NullPointerException(desc) _NullPointerException(__FILE__, __LINE__, desc)
242
243
252 {
253 public:
254 _TypeException(const string & desc_str, const string & type,
255 const string & file = "unknown", int line = 0)
256 : E2Exception(file, line, desc_str, type) {}
257
258 const char *name() const { return "TypeException"; }
259
260 };
261#define TypeException(desc, type) _TypeException(desc, type, __FILE__, __LINE__)
262
263
270 {
271 public:
272 template <class T>
273 _InvalidValueException(T val, const string& file = "unknown",
274 int line = 0, const string& desc_str = "")
275 : E2Exception(file, line, desc_str)
276 {
277 stringstream ss;
278 ss << val;
279 objname = ss.str();
280 }
281 const char *name() const { return "InvalidValueException"; }
282
283 };
284
285#define InvalidValueException(val, desc) \
286 _InvalidValueException(val, __FILE__, __LINE__, desc)
287
288
295 {
296 public:
297 _InvalidStringException(const string& str, const string& file = "unknown",
298 int line = 0, const string& desc_str = "")
299 : E2Exception(file, line, desc_str)
300 {
301 objname = str;
302 }
303 const char *name() const { return "InvalidStringException"; }
304
305 };
306#define InvalidStringException(str, desc) \
307 _InvalidStringException(str, __FILE__, __LINE__, desc)
308
309
318 {
319 public:
320 _OutofRangeException(int low, int high, int input,
321 const string& file = "unknown",
322 int line = 0, const string & desc_str = "",
323 const string& objname_str = "")
324 : E2Exception(file, line, desc_str, objname_str)
325 {
326 stringstream ss;
327 ss << input << " out of range [" << low << "," << high << "]";
328 desc = ss.str();
329 }
330
331 const char *name() const { return "OutofRangeException"; }
332
333 };
334#define OutofRangeException(low, high, input, objname) \
335 _OutofRangeException(low, high, input, __FILE__, __LINE__, objname)
336
337
339 {
340 public:
341 _InvalidCallException(const string& file = "unknown",
342 int line = 0, const string& desc_str = "")
343 : E2Exception(file, line, desc_str) {}
344
345 const char *name() const { return "InvalidCallException"; }
346
347 };
348#define InvalidCallException(desc) _InvalidCallException(__FILE__, __LINE__, desc)
349
350
351 /***/
353 {
354 public:
355 _InvalidParameterException(const string& file = "unknown",
356 int line = 0, const string& desc_str = "")
357 : E2Exception(file, line, desc_str) {}
358
359 const char *name() const { return "InvalidParameterException"; }
360 };
361#define InvalidParameterException(desc) _InvalidParameterException(__FILE__, __LINE__, desc)
362
366 {
367 public:
368 _EmptyContainerException(const string& file = "unknown",
369 int line = 0, const string& desc_str = "")
370 : E2Exception(file, line, desc_str) {}
371
372 const char *name() const { return "EmptyContainerException"; }
373 };
374#define EmptyContainerException(desc) _EmptyContainerException(__FILE__, __LINE__, desc)
375
379 {
380 public:
381 _BadAllocException(const string& file = "unknown",
382 int line = 0, const string& desc_str = "")
383 : E2Exception(file, line, desc_str) {}
384
385 const char *name() const { return "BadAllocException"; }
386 };
387#define BadAllocException(desc) _BadAllocException(__FILE__, __LINE__, desc)
392 {
393 public:
394 _UnexpectedBehaviorException(const string& file = "unknown",
395 int line = 0, const string& desc_str = "")
396 : E2Exception(file, line, desc_str) {}
397
398 const char *name() const { return "UnexpectedBehaviorException"; }
399 };
400#define UnexpectedBehaviorException(desc) _UnexpectedBehaviorException(__FILE__, __LINE__, desc)
401}
402
403
404#endif
E2Exception class is the parent class of all EMAN2 E2Exceptions.
Definition: exception.h:67
virtual const char * what() const
The E2Exception information.
Definition: exception.cpp:36
virtual const char * name() const
The name of this E2Exception class.
Definition: exception.h:103
E2Exception(const string &file="", int line=0, const string &desc_str="", const string &objname_str="")
Contructor.
Definition: exception.h:77
virtual ~E2Exception()
Definition: exception.h:91
Used when memory allocation goes wrong... i.e.
Definition: exception.h:379
const char * name() const
The name of this E2Exception class.
Definition: exception.h:385
_BadAllocException(const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:381
Used when an argument container is empty, such as a vector.
Definition: exception.h:366
const char * name() const
The name of this E2Exception class.
Definition: exception.h:372
_EmptyContainerException(const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:368
Used when a file access error occurs.
Definition: exception.h:175
_FileAccessException(const string &filename_str, const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:177
const char * name() const
The name of this E2Exception class.
Definition: exception.h:184
Used when an image is not in the expected dimension.
Definition: exception.h:157
const char * name() const
The name of this E2Exception class.
Definition: exception.h:163
_ImageDimensionException(const string &desc_str, const string &file="unknown", int line=0)
Definition: exception.h:159
Used when an image is not in the expected format.
Definition: exception.h:137
const char * name() const
The name of this E2Exception class.
Definition: exception.h:144
_ImageFormatException(const string &desc_str, const string &file="unknown", int line=0)
Definition: exception.h:139
Used when an error occurs at image reading time.
Definition: exception.h:195
_ImageReadException(const string &imagename, const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:197
const char * name() const
The name of this E2Exception class.
Definition: exception.h:201
Used when an error occurs at image writing time.
Definition: exception.h:214
_ImageWriteException(const string &imagename, const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:216
const char * name() const
The name of this E2Exception class.
Definition: exception.h:220
_InvalidCallException(const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:341
const char * name() const
The name of this E2Exception class.
Definition: exception.h:345
_InvalidParameterException(const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:355
const char * name() const
The name of this E2Exception class.
Definition: exception.h:359
Used when an invalid (format) string is given.
Definition: exception.h:295
_InvalidStringException(const string &str, const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:297
const char * name() const
The name of this E2Exception class.
Definition: exception.h:303
Used when an invalid integer value is given.
Definition: exception.h:270
const char * name() const
The name of this E2Exception class.
Definition: exception.h:281
_InvalidValueException(T val, const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:273
Used when an object type, like an EMObject type, doesn't exist.
Definition: exception.h:119
const char * name() const
The name of this E2Exception class.
Definition: exception.h:127
_NotExistingObjectException(const string &objname_str, const string &file="unknown", int line=0, const string &desc_str="none")
Definition: exception.h:121
Used when a NULL is given to a pointer that should not be NULL.
Definition: exception.h:232
_NullPointerException(const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:234
const char * name() const
The name of this E2Exception class.
Definition: exception.h:238
Used when the given value is out of range.
Definition: exception.h:318
const char * name() const
The name of this E2Exception class.
Definition: exception.h:331
_OutofRangeException(int low, int high, int input, const string &file="unknown", int line=0, const string &desc_str="", const string &objname_str="")
Definition: exception.h:320
Used when a type cast error occurs.
Definition: exception.h:252
_TypeException(const string &desc_str, const string &type, const string &file="unknown", int line=0)
Definition: exception.h:254
const char * name() const
The name of this E2Exception class.
Definition: exception.h:258
Used when internal behavior is unexpected A generic kind of exception.
Definition: exception.h:392
_UnexpectedBehaviorException(const string &file="unknown", int line=0, const string &desc_str="")
Definition: exception.h:394
const char * name() const
The name of this E2Exception class.
Definition: exception.h:398
E2Exception class.
Definition: aligner.h:40