test_exception_handlers.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright Daniel Wallin 2008. 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. #include "test.hpp"
  5. #include <luabind/luabind.hpp>
  6. #include <luabind/exception_handler.hpp>
  7. struct my_exception {};
  8. void translate_my_exception(lua_State* L, my_exception const&)
  9. {
  10. lua_pushstring(L, "my_exception");
  11. }
  12. struct derived_std_exception : std::exception
  13. {
  14. char const* what() const throw()
  15. {
  16. return "derived_std_exception";
  17. }
  18. };
  19. void translate_derived_exception(lua_State* L, derived_std_exception const&)
  20. {
  21. lua_pushstring(L, "derived_std_exception");
  22. }
  23. void raise()
  24. {
  25. throw my_exception();
  26. }
  27. void raise_derived()
  28. {
  29. throw derived_std_exception();
  30. }
  31. void test_main(lua_State* L)
  32. {
  33. using namespace luabind;
  34. register_exception_handler<my_exception>(&translate_my_exception);
  35. module(L) [
  36. def("raise", &raise),
  37. def("raise_derived", &raise_derived)
  38. ];
  39. DOSTRING(L,
  40. "status, msg = pcall(raise)\n"
  41. "assert(status == false)\n"
  42. "assert(msg == 'my_exception')\n");
  43. DOSTRING(L,
  44. "status, msg = pcall(raise_derived)\n"
  45. "assert(status == false)\n"
  46. "assert(msg == 'std::exception: \\'derived_std_exception\\'')\n");
  47. register_exception_handler<derived_std_exception>(&translate_derived_exception);
  48. DOSTRING(L,
  49. "status, msg = pcall(raise_derived)\n"
  50. "assert(status == false)\n"
  51. "assert(msg == 'derived_std_exception')\n");
  52. }