[LLVMdev] RFC: Exception Handling Proposal II

Duncan Sands baldrick at free.fr
Thu Nov 25 04:01:44 PST 2010


Hi Renato,

>> I don't understand what you are saying.  Cleanups (e.g. destructors)
>
> Hi Duncan,
>
> Cleanup landing pads normally call destructors, but they're not a
> destructor themselves. I'm simply saying that compiler generated
> blocks (such as cleanups) should never depend on user variables.

I see what you are saying now.  Unfortunately optimizations such as
inlining can result in code ending up in cleanup landing pads.

>> In Ada you can throw and exception inside a destructor and it does not lead
>> to program termination.
>
> Ok, sorry. I should stop thinking C++ here.... it's difficult, but
> I'll try... ;)

OK :)  Essentially what happens is as follows: when running destructors
when exiting a scope (which may be a nested scope), if a destructor throws
an exception then any remaining destructors are first run, then the
Program_Error exception is thrown at the point of the scope exit.  This
may be caught by an enclosing handler.

For example, in pseudo C++ code:

{
    ... do some stuff ...

    {
       ... declare a variable A with a destructor that does not throw
       ... declare a variable B with a destructor that throws ...
       ... do some stuff ...
    } // end of scope; when execution reaches here, the destructor for B
      // is run, throwing an exception.  The exception is intercepted and
      // the destructor for A is run.  Once the destructor for A has run,
      // Program_Error is thrown here.

    ... do some stuff (never executed due to Program_Error being thrown) ...
} catch (Program_Error) { // The Program_Error thrown above is caught here.
   print("evil destructors can't hurt me!");
}

Ciao,

Duncan.



More information about the llvm-dev mailing list