GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Will return something after error is thrown and finally executed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Will return something after error is thrown and finally executed?" Watch "Will return something after error is thrown and finally executed?" New topic
Author

Will return something after error is thrown and finally executed?

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451

Hi, I made up some code to play with. In the above code, when assert is false, it first throws an assertion error. According to KB, the finally block is executed before the return 1 statement. So, the "finally is here" is printed and the assertion error is output followed by 12.
The output is :
Finally is here.
Exception in Thread "main" java.lang.AssertionError 12

But how about the return?

The method will return 1 and assign 1 to a?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4398
    
    8

It won't return anything. The AssertionError is never caught, and so just propagates up through the stack. Your code will never reach the point of assigning anything to a.

AssertionError inherits from Error, which means that it's not an Exception. So it's not caught in your code. If you changed line 17 to catch Error, then execution would continue after the try/catch/finally block, which would mean the method would return 0.

But it's never going to return 1 if the assertion error is thrown.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, thanks for your reply.
I changed this program like this:

public class Assert{

public static void main (String...args){
int a =go();
System.out.println(a);
}
private static int go(){

int x = 1;
int j ;
try{
assert (x>1) : j=12;
return 0;
}
catch (Exception e){
System.out.println("Error is thrown here.");
return 1;
}
finally{
System.out.println("finally is here");
return 2;
}
}
}


The assertion error is not caught and the finally block is executed and it return 2!
I thought it won't return any output except a assertion error message.
The output is
finally is here.
2
Can anyone explain why the output is 2 instead of printing out the error message?Thanks.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3606
    
  15
Helen Ma wrote:Can anyone explain why the output is 2 instead of printing out the error message?Thanks.

Matthew's already explained why the error message doesn't get printed. The reason it prints 2 is that if you put a return or throws statement in a finally clause, it will take precedence over any earlier return or throws statement. So instead of throwing the AssertionError, your method now returns 2.

And it's better to use the Code button rather than the Quote button when posting code. It formats much better.


Joanne
 
GeeCON Prague 2014
 
subject: Will return something after error is thrown and finally executed?