• Post Reply Bookmark Topic Watch Topic
  • New Topic

Quick Q on Try/Catch/Finally  RSS feed

 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the difference between this:

and this:

On execution there is no difference. They return the same results.

What does the first code example allow that the second doesn't i.e. is there any inherent advantage to putting "always executed" code in the 'finally' OR is it just a case of trying to standardise error handling?

PaulC.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
What does the first code example allow that the second doesn't i.e. is there any inherent advantage to putting "always executed" code in the 'finally' OR is it just a case of trying to standardise error handling?


The second case does not execute the "always executed" code, in the case of exceptions that are not handle -- for example, some sort of unchecked exception that is thrown from either the try or catch block.

Henry
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me change your code a little bit:

Now if you execute this and there is no exception, you will not see "Out of try/catch block".  That is, the "finally" block is executed regardless of what the "try" or "catch" blocks do -- with one exception: if they execute System.exit(0).
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Let me change your code a little bit:

Now if you execute this and there is no exception, you will not see "Out of try/catch block".  That is, the "finally" block is executed regardless of what the "try" or "catch" blocks do -- with one exception: if they execute System.exit(0).


Ah, ok. That makes it clearer. What you're saying is that the 'finally' code always executes, even if you exit out of the try/catch with a return, or as Henry says, receive an unhandled exception.

Thanks, knew there was a reason. Appreciate the replies.

PaulC.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
Ah, ok. That makes it clearer. What you're saying is that the 'finally' code always executes, even if you exit out of the try/catch with a return, or as Henry says, receive an unhandled exception.

Using finally with return, meaning the ability to run code after a value is returned, actually has cool possibilities. For example, I can do a post increment getter like this...


And yes, I know, it could be done more simply with just a "return seqNum++;" ... ... but what if the post return operation was more complex than just an increment?

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!