• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Quick Q on Try/Catch/Finally

 
Ranch Hand
Posts: 99
1
Eclipse IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic