Merge branch '0.7.1'
[luabind.git] / src / exception_handler.cpp
blob4e9d0280fccac579eb5d18c559daf4e9daaf3d5e
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)
5 #define LUABIND_SOURCE
7 #include <luabind/config.hpp>
8 #include <luabind/exception_handler.hpp>
9 #include <luabind/error.hpp>
10 #include <stdexcept>
12 namespace luabind { namespace detail {
14 namespace
16 exception_handler_base* handler_chain = 0;
18 void push_exception_string(lua_State* L, char const* exception, char const* what)
20 lua_pushstring(L, exception);
21 lua_pushstring(L, ": '");
22 lua_pushstring(L, what);
23 lua_pushstring(L, "'");
24 lua_concat(L, 4);
28 void exception_handler_base::try_next(lua_State* L) const
30 if (next)
31 next->handle(L);
32 else
33 throw;
36 LUABIND_API void handle_exception_aux(lua_State* L)
38 try
40 if (handler_chain)
41 handler_chain->handle(L);
42 else
43 throw;
45 catch (error const&)
47 catch (std::logic_error const& e)
49 push_exception_string(L, "std::logic_error", e.what());
51 catch (std::runtime_error const& e)
53 push_exception_string(L, "std::runtime_error", e.what());
55 catch (std::exception const& e)
57 push_exception_string(L, "std::exception", e.what());
59 catch (char const* str)
61 push_exception_string(L, "c-string", str);
63 catch (...)
65 lua_pushstring(L, "Unknown C++ exception");
69 LUABIND_API void register_exception_handler(exception_handler_base* handler)
71 if (!handler_chain) handler_chain = handler;
72 else
74 exception_handler_base* p = handler_chain;
76 for (; p->next; p = p->next);
78 handler->next = 0;
79 p->next = handler;
83 }} // namespace luabind::detail