[Soot-list] Extra athrow instruction in a finally block

mbatch at cs.mcgill.ca mbatch at cs.mcgill.ca
Thu Mar 6 16:33:58 EST 2008


> The method a() could throw an Error, too.  Did you try compiling this
> code with "catch(Throwable t)" instead?  If the compiler is smart, your
> athrow might go away.  Otherwise, it might become unreachable code.

You are correct, Richard, and I should have mentioned this. It isn't
really the catch block that is implicitly protected by a try/catch, it is
the entire try/catch construct that is protected (from a Throwable object,
the most generic)..

So even if the original try block catches a Throwable, you will STILL have
an implicitly generated try/catch(Throwable) that ensures that the finally
block runs (and then it throws the Throwable up the call stack, because
the throwable is NOT explicitly caught).

If you do catch the Throwable, as Richard suggested, instead of an
Exception, then that explicit catch will be caught, and the finally will
be run. There will still be a separate implicit catch of anything else
thrown within the catch block itself.

As an illustrative example, the "source code" representation would sort of
look like this (but it is a rough estimation, so gimme some slack):

try {  // implicit try
   try // explicit try
   {
      a();
   } catch (Throwable t1) { // explicit catch

   }
} catch (Throwable t2) { // implicit catch

   // explicit finally duplicated in implicit catch
   // re-throws NON explicit t2!!!
   System.out.println();
   throw t2;
}
// explicit finally runs after all try/catches have processed themselves.
System.out.println();


Michael



More information about the Soot-list mailing list