[1] | 1 | /*!\file exceptions.h
|
---|
| 2 | * \brief: two types of exceptions are handled for now. Errors, and
|
---|
| 3 | * warnings. Those exceptions are trapped provided the matlab modules
|
---|
| 4 | * are started using MODULEBOOT, and ended using MODULEEND. These are
|
---|
| 5 | * macros hiding try, catch statements. This header file defines our
|
---|
| 6 | * own exceptions
|
---|
| 7 | */
|
---|
| 8 |
|
---|
[14914] | 9 | #ifndef _MY_EXCEPTIONS_H_
|
---|
| 10 | #define _MY_EXCEPTIONS_H_
|
---|
[1] | 11 |
|
---|
| 12 | #include <exception>
|
---|
| 13 | #include <string>
|
---|
[14914] | 14 | #include <iostream>
|
---|
| 15 | #include <sstream>
|
---|
| 16 | #include <iomanip>
|
---|
| 17 |
|
---|
[1] | 18 | using namespace std;
|
---|
| 19 |
|
---|
[14914] | 20 | #ifdef HAVE_CONFIG_H
|
---|
| 21 | #include <config.h>
|
---|
| 22 | #else
|
---|
| 23 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
| 24 | #endif
|
---|
[13623] | 25 |
|
---|
[24477] | 26 | /*Only include forward declaration to save compile time*/
|
---|
| 27 | #include <iosfwd>
|
---|
| 28 | #include <sstream>
|
---|
| 29 |
|
---|
[26976] | 30 | /*macros: (should move somewhere else)*/
|
---|
| 31 | #include "../io/Comm/IssmComm.h"
|
---|
[14914] | 32 | /* _assert_ {{{*/
|
---|
| 33 | /*Assertion macro: do nothing if macro _ISSM_DEBUG_ undefined*/
|
---|
| 34 | #ifdef _ISSM_DEBUG_
|
---|
| 35 | #define _assert_(statement)\
|
---|
[27776] | 36 | if (!(statement)) _error_("Assertion \""<<#statement<<"\" failed, please report bug at "<<PACKAGE_BUGREPORT)
|
---|
[14914] | 37 | #else
|
---|
| 38 | #define _assert_(ignore)\
|
---|
| 39 | ((void) 0)
|
---|
| 40 | #endif
|
---|
| 41 | /*}}}*/
|
---|
| 42 | /* _error_ {{{*/
|
---|
| 43 | /*new Error exception macro*/
|
---|
| 44 | #ifdef _INTEL_WIN_
|
---|
| 45 | #define _error_(StreamArgs)\
|
---|
| 46 | do{std::ostringstream aLoNgAnDwEiRdLoCaLnAmeFoRtHiSmAcRoOnLy; \
|
---|
| 47 | aLoNgAnDwEiRdLoCaLnAmeFoRtHiSmAcRoOnLy << StreamArgs << std::ends; \
|
---|
| 48 | throw ErrorException(aLoNgAnDwEiRdLoCaLnAmeFoRtHiSmAcRoOnLy.str());}while(0)
|
---|
| 49 | #else
|
---|
| 50 | #define _error_(StreamArgs)\
|
---|
| 51 | do{std::ostringstream aLoNgAnDwEiRdLoCaLnAmeFoRtHiSmAcRoOnLy; \
|
---|
| 52 | aLoNgAnDwEiRdLoCaLnAmeFoRtHiSmAcRoOnLy << StreamArgs << std::ends; \
|
---|
[26976] | 53 | throw ErrorException(IssmComm::GetRank(),__FILE__,__func__,__LINE__,aLoNgAnDwEiRdLoCaLnAmeFoRtHiSmAcRoOnLy.str());}while(0)
|
---|
[14914] | 54 | #endif
|
---|
| 55 | /*}}}*/
|
---|
| 56 | /* ExceptionTrapBegin/ExceptionTrapEnd {{{*/
|
---|
| 57 |
|
---|
| 58 | /*The following macros hide the error exception handling in a matlab module. Just put
|
---|
| 59 | * ExceptionTrapBegin(); and ExceptionTrapEnd(); at the beginning and end of a module, and c++ exceptions
|
---|
| 60 | * will be trapped. Really nifty!*/
|
---|
| 61 |
|
---|
| 62 | #define ExceptionTrapBegin(); \
|
---|
| 63 | try{
|
---|
| 64 |
|
---|
| 65 | #define ExceptionTrapEnd(); }\
|
---|
| 66 | catch(ErrorException &exception){\
|
---|
| 67 | exception.Report();\
|
---|
[22634] | 68 | return 1;\
|
---|
[14914] | 69 | }\
|
---|
[17513] | 70 | catch(exception& e) {\
|
---|
[15104] | 71 | _printf_("Standard exception: " << e.what() << "\n\n");\
|
---|
[22634] | 72 | return 1;\
|
---|
[14914] | 73 | }\
|
---|
| 74 | catch(...){\
|
---|
[15104] | 75 | _printf_("An unexpected error occurred \n\n");\
|
---|
[22634] | 76 | return 1;\
|
---|
[14914] | 77 | }
|
---|
| 78 | /*}}}*/
|
---|
| 79 |
|
---|
| 80 | /*ISSM exception class: */
|
---|
[26976] | 81 | class ErrorException: public exception{ /*{{{*/
|
---|
[14914] | 82 |
|
---|
[16155] | 83 | char* what_str;
|
---|
| 84 | char* function_name;
|
---|
| 85 | char* file_name;
|
---|
| 86 | int file_line;
|
---|
[26976] | 87 | int rank;
|
---|
[1] | 88 |
|
---|
[12079] | 89 | public:
|
---|
[26976] | 90 | /*Windows*/
|
---|
| 91 | ErrorException(const string &what_arg);
|
---|
| 92 | /*Linux/macOS*/
|
---|
| 93 | ErrorException(int what_rank,const string &what_file,const string& what_function,int what_line,const string& what_arg);
|
---|
[1] | 94 | ~ErrorException() throw();
|
---|
| 95 | virtual const char *what() const throw();
|
---|
[13363] | 96 | void Report() const;
|
---|
[19701] | 97 | const char* WrapperReport() const;
|
---|
[1] | 98 |
|
---|
| 99 | };
|
---|
[14914] | 100 | /*}}}*/
|
---|
[1] | 101 | #endif
|
---|