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...