PDA

View Full Version : can 'finally' block catch Error?


vysh
05-29-2009, 07:18 AM
Hi all,

I have the following code block:

class exceptiondemo
{
static void test() throws Error
{
if (true)
throw new AssertionError();
System.out.print("test ");
}
public static void main(String[] args)
{
try
{
test();
}
catch (Exception ex)
{
System.out.print("exception ");
}
finally
{
}
System.out.print("end ");
}
}

Here a throwable is thrown by main, because the AssertionError is not caught. If the catch block would have been, catch(AssertionError ex), then the exception would have been caught. But I have used finally block. Can't the finally block catch this error?

servlet
05-29-2009, 12:01 PM
Nope, finally will not block the error/exception , error/exception will be propagated up the thread.
Finally block contains the code that must be executed no matter weather there was an error/exception or not.