Nested exceptions helpers
Overview
MRPT exceptions carry the call stack at the throw point, so an error raised deep inside a library can be traced back from the top-level catch. All these helpers are defined in <mrpt/core/exceptions.h>.
Throwing and asserting
THROW_EXCEPTION(msg),THROW_EXCEPTION_FMT(fmt, ...): throw astd::logic_errorwith the message, source file, line, function name and call stack.THROW_TYPED_EXCEPTION(msg, type)throws another exception class.ASSERT_(cond),ASSERTMSG_(cond, msg): throw if the condition is false.ASSERT_EQUAL_(a, b),ASSERT_NOT_EQUAL_(),ASSERT_LT_(),ASSERT_LE_(),ASSERT_GT_(),ASSERT_GE_(),ASSERT_NEAR_(a, b, tol): comparisons whose error message includes the values of both operands.ASSERTDEB_(cond)and the otherASSERTDEB_*variants: like the above, but only checked if_DEBUGis defined.
Tracing exceptions through the call stack
Wrapping the body of a function between MRPT_START and MRPT_END catches any std::exception thrown inside it (also from the standard library or third-party code) and rethrows it wrapped in an mrpt::ExceptionWithCallBack, which records the call stack. Exceptions already wrapped are passed through unchanged. MRPT_END_WITH_CLEAN_UP(stuff) also runs some code before rethrowing.
At the top level, print the full report with e.what() or mrpt::exception_to_str(). File names and line numbers of each frame are shown when debug symbols are available.
Example
See: core_exceptions_example/main.cpp
#include <mrpt/core/exceptions.h> #include <iostream> void test_except_3rd_lvl() { MRPT_START THROW_EXCEPTION("Aw!"); MRPT_END } void test_except_2nd_lvl() { MRPT_START test_except_3rd_lvl(); MRPT_END } void test_except_toplevel() { MRPT_START test_except_2nd_lvl(); MRPT_END } void test_except_2nd_lvl_bis() { MRPT_START std::vector<int> x; x.resize(2); (void)x.at(10); // throws MRPT_END } void test_except_toplevel_bis() { test_except_2nd_lvl_bis(); } int main() { try { test_except_toplevel(); return 0; } catch (const std::exception& e) { std::cerr << e.what(); } try { test_except_toplevel_bis(); return 0; } catch (const std::exception& e) { std::cerr << e.what(); } return 0; }
Output:
==== MRPT exception ==== Message: Aw! Call stack backtrace: [0 ] /home/jlblanco/code/mrpt/mrpt_examples_cpp/core_exceptions_example/main.cpp:21 test_except_3rd_lvl() [1 ] /home/jlblanco/code/mrpt/mrpt_examples_cpp/core_exceptions_example/main.cpp:28 test_except_2nd_lvl() [2 ] /home/jlblanco/code/mrpt/mrpt_examples_cpp/core_exceptions_example/main.cpp:35 test_except_toplevel() [3 ] /home/jlblanco/code/mrpt/mrpt_examples_cpp/core_exceptions_example/main.cpp:54 main [4 ] (unknown file) __libc_start_main [5 ] (unknown file) _start ==== MRPT exception ==== vector::_M_range_check: __n (which is 10) >= this->size() (which is 2) Call stack backtrace: [0 ] /home/jlblanco/code/mrpt/mrpt_examples_cpp/core_exceptions_example/main.cpp:45 test_except_2nd_lvl_bis() [1 ] /home/jlblanco/code/mrpt/mrpt_examples_cpp/core_exceptions_example/main.cpp:48 test_except_toplevel_bis() [2 ] (unknown file) __libc_start_main [3 ] (unknown file) _start
See also Example: core_backtrace_example, which captures the current call stack with mrpt::callStackBackTrace() without throwing.