1/* ----------------------------------------------------------------------------- 2 * See the LICENSE file for information on copyright, usage and redistribution 3 * of SWIG, and the README file for authors - http://www.swig.org/release.html. 4 * 5 * exceptions.swg 6 * 7 * This SWIG library file provides language independent exception handling 8 * ----------------------------------------------------------------------------- */ 9 10%include <typemaps/swigmacros.swg> 11 12 13/* macros for error manipulation */ 14#define %nullref_fmt() "invalid null reference " 15#define %varfail_fmt(_type,_name) "in variable '"`_name`"' of type '"`_type`"'" 16#ifndef %argfail_fmt 17#define %argfail_fmt(_type,_name,_argn) "in method '" `_name` "', argument " `_argn`" of type '" `_type`"'" 18#endif 19#define %outfail_fmt(_type) "in output value of type '"_type"'" 20#ifndef %argnullref_fmt 21#define %argnullref_fmt(_type,_name,_argn) %nullref_fmt() %argfail_fmt(_type, _name, _argn) 22#endif 23#define %varnullref_fmt(_type,_name) %nullref_fmt() %varfail_fmt(_type, _name) 24#define %outnullref_fmt(_type) %nullref_fmt() %outfail_fmt(_type) 25 26/* setting an error */ 27#define %error(code,msg...) SWIG_Error(code, msg) 28#define %type_error(msg...) SWIG_Error(SWIG_TypeError, msg) 29 30 31 32%insert("runtime") { 33 34%define_as(SWIG_exception_fail(code, msg), %block(%error(code, msg); SWIG_fail)) 35 36%define_as(SWIG_contract_assert(expr, msg), if (!(expr)) { %error(SWIG_RuntimeError, msg); SWIG_fail; } else) 37 38} 39 40#ifdef __cplusplus 41/* 42 You can use the SWIG_CATCH_STDEXCEPT macro with the %exception 43 directive as follows: 44 45 %exception { 46 try { 47 $action 48 } 49 catch (my_except& e) { 50 ... 51 } 52 SWIG_CATCH_STDEXCEPT // catch std::exception 53 catch (...) { 54 SWIG_exception_fail(SWIG_UnknownError, "Unknown exception"); 55 } 56 } 57*/ 58%{ 59#include <stdexcept> 60%} 61%define SWIG_CATCH_STDEXCEPT 62 /* catching std::exception */ 63 catch (std::invalid_argument& e) { 64 SWIG_exception_fail(SWIG_ValueError, e.what() ); 65 } catch (std::domain_error& e) { 66 SWIG_exception_fail(SWIG_ValueError, e.what() ); 67 } catch (std::overflow_error& e) { 68 SWIG_exception_fail(SWIG_OverflowError, e.what() ); 69 } catch (std::out_of_range& e) { 70 SWIG_exception_fail(SWIG_IndexError, e.what() ); 71 } catch (std::length_error& e) { 72 SWIG_exception_fail(SWIG_IndexError, e.what() ); 73 } catch (std::runtime_error& e) { 74 SWIG_exception_fail(SWIG_RuntimeError, e.what() ); 75 } catch (std::exception& e) { 76 SWIG_exception_fail(SWIG_SystemError, e.what() ); 77 } 78%enddef 79%define SWIG_CATCH_UNKNOWN 80 catch (std::exception& e) { 81 SWIG_exception_fail(SWIG_SystemError, e.what() ); 82 } 83 catch (...) { 84 SWIG_exception_fail(SWIG_UnknownError, "unknown exception"); 85 } 86%enddef 87 88 89#endif /* __cplusplus */ 90