exception_handler.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright Daniel Wallin 2005. Use, modification and distribution is
  2. // subject to the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #define LUABIND_BUILDING
  5. #include <luabind/config.hpp>
  6. #include <luabind/exception_handler.hpp>
  7. #include <luabind/error.hpp>
  8. #include <stdexcept>
  9. #ifndef LUABIND_NO_EXCEPTIONS
  10. namespace luabind { namespace detail {
  11. namespace
  12. {
  13. exception_handler_base* handler_chain = 0;
  14. void push_exception_string(lua_State* L, char const* exception, char const* what)
  15. {
  16. lua_pushstring(L, exception);
  17. lua_pushstring(L, ": '");
  18. lua_pushstring(L, what);
  19. lua_pushstring(L, "'");
  20. lua_concat(L, 4);
  21. }
  22. }
  23. void exception_handler_base::try_next(lua_State* L) const
  24. {
  25. if (next)
  26. next->handle(L);
  27. else
  28. throw;
  29. }
  30. LUABIND_API void handle_exception_aux(lua_State* L)
  31. {
  32. try
  33. {
  34. if (handler_chain)
  35. handler_chain->handle(L);
  36. else
  37. throw;
  38. }
  39. catch (error const&)
  40. {}
  41. catch (std::logic_error const& e)
  42. {
  43. push_exception_string(L, "std::logic_error", e.what());
  44. }
  45. catch (std::runtime_error const& e)
  46. {
  47. push_exception_string(L, "std::runtime_error", e.what());
  48. }
  49. catch (std::exception const& e)
  50. {
  51. push_exception_string(L, "std::exception", e.what());
  52. }
  53. catch (char const* str)
  54. {
  55. push_exception_string(L, "c-string", str);
  56. }
  57. catch (...)
  58. {
  59. lua_pushstring(L, "Unknown C++ exception");
  60. }
  61. }
  62. LUABIND_API void register_exception_handler(exception_handler_base* handler)
  63. {
  64. if (!handler_chain) handler_chain = handler;
  65. else
  66. {
  67. exception_handler_base* p = handler_chain;
  68. for (; p->next; p = p->next);
  69. handler->next = 0;
  70. p->next = handler;
  71. }
  72. }
  73. }} // namespace luabind::detail
  74. #endif // LUABIND_NO_EXCEPTIONS