This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Will return something after error is thrown and finally executed?

 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic