EMAN2
exception.h
Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Liwei Peng, 07/12/2004 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #ifndef eman__exception_h__
00037 #define eman__exception_h__ 1
00038 
00039 #ifdef _WIN32
00040         #pragma warning(disable:4996)
00041 #endif  //_WIN32
00042 
00043 #include <log.h>
00044 
00045 #include <string>
00046 
00047 using std::string;
00048 
00049 #include <sstream>
00050 using std::stringstream;
00051 
00071 namespace EMAN {
00072 
00075     class E2Exception : public std::exception {
00076     public:
00085                 explicit E2Exception(const string& file = "", int line = 0,
00086                                   const string& desc_str = "", const string& objname_str = "")
00087                         : filename(file), linenum(line), desc(desc_str), objname(objname_str)
00088                 {
00089                         // Log the message
00090                         stringstream ss;
00091                         string line_str;
00092                         ss << line;
00093                         ss >> line_str;
00094 // We shouldn't log all exceptions, since they are frequently used intentionally, with no implied error
00095 //                      string message = "File " + file + " line " + line_str + " description " + desc_str + " " + objname_str;
00096 //                      LOGERR( message.c_str() );
00097                 }
00098 
00099                 virtual ~E2Exception() throw() {}
00100 
00106                 virtual const char *what() const throw();
00107 
00111                 virtual const char *name() const { return "Exception"; }
00112         protected:
00113                 string filename;
00114                 int linenum;
00115                 string desc;
00116                 string objname;
00117     };
00118 
00119 
00126         class _NotExistingObjectException : public E2Exception
00127         {
00128         public:
00129                 _NotExistingObjectException(const string& objname_str,
00130                                                                         const string& file = "unknown",
00131                                                                         int line = 0,
00132                                                                         const string& desc_str = "none")
00133                         : E2Exception(file, line, desc_str, objname_str) {}
00134 
00135                 const char *name() const { return "NotExistingObjectException"; }
00136 
00137         };
00138 #define NotExistingObjectException(objname, desc)  \
00139  _NotExistingObjectException(objname, __FILE__, __LINE__, desc)
00140 
00141 
00144         class _ImageFormatException : public E2Exception
00145         {
00146         public:
00147                 _ImageFormatException(const string& desc_str,
00148                                                           const string& file = "unknown",
00149                                                           int line = 0)
00150                         : E2Exception(file, line, desc_str) {}
00151 
00152                 const char *name() const { return "ImageFormatException"; }
00153 
00154         };
00155 #define ImageFormatException(desc) _ImageFormatException(desc, __FILE__, __LINE__)
00156 
00157 
00164         class _ImageDimensionException : public E2Exception
00165         {
00166         public:
00167                 _ImageDimensionException(const string& desc_str, const string& file = "unknown",
00168                                                                  int line = 0)
00169                         : E2Exception(file, line, desc_str) {}
00170 
00171                 const char *name() const { return "ImageDimensionException"; }
00172 
00173         };
00174 #define ImageDimensionException(desc) _ImageDimensionException(desc, __FILE__, __LINE__)
00175 
00176 
00182         class _FileAccessException : public E2Exception
00183         {
00184         public:
00185                 _FileAccessException(const string& filename_str, const string& file = "unknown",
00186                                                          int line = 0, const string& desc_str = "")
00187                         : E2Exception(file, line, desc_str, filename_str)
00188                 {
00189                         desc = "cannot access file '" + filename_str + "'";
00190                 }
00191 
00192                 const char *name() const { return "FileAccessException"; }
00193 
00194         };
00195 #define FileAccessException(filename) _FileAccessException(filename, __FILE__, __LINE__)
00196 
00202         class _ImageReadException : public E2Exception
00203         {
00204         public:
00205                 _ImageReadException(const string& imagename, const string& file = "unknown",
00206                                                         int line = 0, const string& desc_str = "")
00207                         : E2Exception(file, line, desc_str, imagename) {}
00208 
00209                 const char *name() const { return "ImageReadException"; }
00210 
00211         };
00212 #define ImageReadException(filename, desc) \
00213  _ImageReadException(filename, __FILE__, __LINE__, desc)
00214 
00215 
00221         class _ImageWriteException : public E2Exception
00222         {
00223         public:
00224                 _ImageWriteException(const string& imagename, const string& file = "unknown",
00225                                                          int line = 0, const string& desc_str = "")
00226                         : E2Exception(file, line, desc_str, imagename) {}
00227 
00228                 const char *name() const { return "ImageWriteException"; }
00229 
00230         };
00231 #define ImageWriteException(imagename, desc) \
00232  _ImageWriteException(imagename, __FILE__, __LINE__, desc)
00233 
00234 
00239         class _NullPointerException : public E2Exception
00240         {
00241         public:
00242                 _NullPointerException(const string& file = "unknown",
00243                                                           int line = 0, const string& desc_str = "")
00244                         : E2Exception(file, line, desc_str) {}
00245 
00246                 const char *name() const { return "NullPointerException"; }
00247 
00248         };
00249 #define NullPointerException(desc) _NullPointerException(__FILE__, __LINE__, desc)
00250 
00251 
00259         class _TypeException : public E2Exception
00260         {
00261         public:
00262                 _TypeException(const string & desc_str, const string & type,
00263                                            const string & file = "unknown", int line = 0)
00264                         : E2Exception(file, line, desc_str, type) {}
00265 
00266                 const char *name() const { return "TypeException"; }
00267 
00268         };
00269 #define TypeException(desc, type) _TypeException(desc, type, __FILE__, __LINE__)
00270 
00271 
00277         class _InvalidValueException : public E2Exception
00278         {
00279         public:
00280                 template <class T>
00281                 _InvalidValueException(T val, const string& file = "unknown",
00282                                                            int line = 0, const string& desc_str = "")
00283                         : E2Exception(file, line, desc_str)
00284                 {
00285                         stringstream ss;
00286                         ss << val;
00287                         objname = ss.str();
00288                 }
00289                 const char *name() const { return "InvalidValueException"; }
00290 
00291         };
00292 
00293 #define InvalidValueException(val, desc) \
00294  _InvalidValueException(val, __FILE__, __LINE__, desc)
00295 
00296 
00302         class _InvalidStringException : public E2Exception
00303         {
00304         public:
00305                 _InvalidStringException(const string& str, const string& file = "unknown",
00306                                                            int line = 0, const string& desc_str = "")
00307                         : E2Exception(file, line, desc_str)
00308                 {
00309                         objname = str;
00310                 }
00311                 const char *name() const { return "InvalidStringException"; }
00312 
00313         };
00314 #define InvalidStringException(str, desc) \
00315  _InvalidStringException(str, __FILE__, __LINE__, desc)
00316 
00317 
00325         class _OutofRangeException : public E2Exception
00326         {
00327         public:
00328                 _OutofRangeException(int low, int high, int input,
00329                                                          const string& file = "unknown",
00330                                                          int line = 0, const string & desc_str = "",
00331                                                          const string& objname_str = "")
00332                         : E2Exception(file, line, desc_str, objname_str)
00333                 {
00334                         stringstream ss;
00335                         ss << input << " out of range [" << low << "," << high << "]";
00336                         desc = ss.str();
00337                 }
00338 
00339                 const char *name() const { return "OutofRangeException"; }
00340 
00341         };
00342 #define OutofRangeException(low, high, input, objname) \
00343  _OutofRangeException(low, high, input,  __FILE__, __LINE__, objname)
00344 
00345 
00346         class _InvalidCallException : public E2Exception
00347         {
00348         public:
00349                 _InvalidCallException(const string& file = "unknown",
00350                                                           int line = 0, const string& desc_str = "")
00351                         : E2Exception(file, line, desc_str) {}
00352 
00353                 const char *name() const { return "InvalidCallException"; }
00354 
00355         };
00356 #define InvalidCallException(desc) _InvalidCallException(__FILE__, __LINE__, desc)
00357 
00358 
00359         /***/
00360         class _InvalidParameterException : public E2Exception
00361         {
00362         public:
00363                 _InvalidParameterException(const string& file = "unknown",
00364                                                                         int line = 0, const string& desc_str = "")
00365                         : E2Exception(file, line, desc_str) {}
00366 
00367                 const char *name() const { return "InvalidParameterException"; }
00368         };
00369 #define InvalidParameterException(desc) _InvalidParameterException(__FILE__, __LINE__, desc)
00370 
00373         class _EmptyContainerException : public E2Exception
00374         {
00375                 public:
00376                         _EmptyContainerException(const string& file = "unknown",
00377                                                                            int line = 0, const string& desc_str = "")
00378                         : E2Exception(file, line, desc_str) {}
00379 
00380                         const char *name() const { return "EmptyContainerException"; }
00381         };
00382 #define EmptyContainerException(desc) _EmptyContainerException(__FILE__, __LINE__, desc)
00383 
00386         class _BadAllocException : public E2Exception
00387         {
00388                 public:
00389                         _BadAllocException(const string& file = "unknown",
00390                                                                          int line = 0, const string& desc_str = "")
00391                         : E2Exception(file, line, desc_str) {}
00392 
00393                         const char *name() const { return "BadAllocException"; }
00394         };
00395 #define BadAllocException(desc) _BadAllocException(__FILE__, __LINE__, desc)
00396 
00399         class _UnexpectedBehaviorException : public E2Exception
00400         {
00401                 public:
00402                         _UnexpectedBehaviorException(const string& file = "unknown",
00403                                                            int line = 0, const string& desc_str = "")
00404                         : E2Exception(file, line, desc_str) {}
00405 
00406                         const char *name() const { return "UnexpectedBehaviorException"; }
00407         };
00408 #define UnexpectedBehaviorException(desc) _UnexpectedBehaviorException(__FILE__, __LINE__, desc)
00409 }
00410 
00411 
00412 #endif