From 36f34d7a4841ded062688bb4ccabe2517d77395a Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Thu, 20 Oct 2005 10:12:40 +0000 Subject: [PATCH] add missing macros git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7694 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Lib/typemaps/exception.swg | 58 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/Lib/typemaps/exception.swg b/Lib/typemaps/exception.swg index 0225bdf04..781f74f58 100644 --- a/Lib/typemaps/exception.swg +++ b/Lib/typemaps/exception.swg @@ -3,7 +3,61 @@ %insert("runtime") { - SWIG_define(SWIG_exception(code, msg), SWIG_block(SWIG_error(code, msg); SWIG_fail)) - SWIG_define(SWIG_contract_assert(expr, msg), if (!(expr)) { SWIG_error(SWIG_RuntimeError, msg); SWIG_fail; } else) + SWIG_define(SWIG_exception(code, msg), + SWIG_block(SWIG_error(code, msg); SWIG_fail)) + + SWIG_define(SWIG_contract_assert(expr, msg), + if (!(expr)) { SWIG_error(SWIG_RuntimeError, msg); SWIG_fail; } else) } + +#ifdef __cplusplus +/* + You can use the SWIG_CATCH_STDEXCEPT macro with the %exception + directive as follows: + + %exception { + try { + $action + } + catch (my_except& e) { + ... + } + SWIG_CATCH_STDEXCEPT // catch std::exception + catch (...) { + SWIG_exception(SWIG_UnknownError, "Unknown exception"); + } + } +*/ +%{ +#include +%} +%define SWIG_CATCH_STDEXCEPT + /* catching std::exception */ + catch (std::invalid_argument& e) { + SWIG_exception(SWIG_ValueError, e.what() ); + } catch (std::domain_error& e) { + SWIG_exception(SWIG_ValueError, e.what() ); + } catch (std::overflow_error& e) { + SWIG_exception(SWIG_OverflowError, e.what() ); + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError, e.what() ); + } catch (std::length_error& e) { + SWIG_exception(SWIG_IndexError, e.what() ); + } catch (std::runtime_error& e) { + SWIG_exception(SWIG_RuntimeError, e.what() ); + } catch (std::exception& e) { + SWIG_exception(SWIG_SystemError, e.what() ); + } +%enddef +%define SWIG_CATCH_UNKNOWN + catch (std::exception& e) { + SWIG_exception(SWIG_SystemError, e.what() ); + } + catch (...) { + SWIG_exception(SWIG_UnknownError, "unknown exception"); + } +%enddef + + +#endif /* __cplusplus */