• Post Reply Bookmark Topic Watch Topic
  • New Topic

try catch finally

 
bob reilly
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked to modified software invoked via a try catch finally block. After modifying the software I thought I may need to exit out of the try block and go to the finally section.

Two questions:
- is exiting a try block good coding practice/
- how would you go about doing it. System.exit will not execute the finally block.

Any URL / examples / alternatives would be appreciated. Thanking you in advance...
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about return?
 
bob reilly
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you provide an example? I provided syntax:

if (ot_lunch.equals(""))
{ return; } // this fails...

Am I having a brain hiccup here? It looks simple and yet...


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see ItDoesntWorkIsUseless.

Is your method supposed to return a value? A return doesn't just "fail"--there's either a compilation error, a logic error, *something*.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether or not to "exit" a try block depends on the situation, code style/standards, etc.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15758
74
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean by "exit out of the try block and go to the finally section"? Can you give a code example that explains what you mean?

The code in the finally-block is normally always executed (unless, for example, you stop the JVM by calling System.exit()). Have a look at this:


 
bob reilly
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. The return syntax worked. And yes - a return of an object was required (David - you were right). Once that was built - I was able to go to 'finally'.

Thank you all for your responding.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!