Throwing and catching exceptions
Exception handling in LLVM IR is closely tied to the platform's support. Here, we will look at the most common type of exception handling using libunwind
. Its full potential is used by C++, so we will look at an example in C++ first, where the bar()
function can throw an int
or a double
value, as follows:
int bar(int x) { Â Â if (x == 1) throw 1; Â Â if (x == 2) throw 42.0; Â Â return x; }
The foo()
function calls bar()
, but only handles a thrown int
value. It also declares that it only throws int
values, as follows:
int foo(int x) throw(int) { Â Â int y = 0; Â Â try { Â Â Â Â y = bar(x); Â Â } Â Â catch (int e) { Â Â Â Â y = e; Â Â } Â Â return y; }
Throwing an exception requires two calls into the runtime library. First, memory for the exception is allocated with a call to __cxa_allocate_exception()
. This function takes the number...