• 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

Escaping the Exception Trap

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guyz,
I have a situation in a java program where I want to exclude a statement from the exception trap.
Below is the example that will make things clear :
--------------------------------------------------------------------------------

--------------------------------------------------------------------------
thankyou

[Edit - added code tags - MB]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid you will have to explain more what you want to do.
If your method call falls in the sequence which must be inside the try block (don’t call it a trap), your method will become incorrect if you move the call. Breaking the try into two blocks may also make the logic incorrect.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You can split the try-catch blocks into two blocks and put the method in between:

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vis kat wrote:Hi guyz,
I have a situation in a java program where I want to exclude a statement from the exception trap.
Below is the example that will make things clear :
--------------------------------------------------------------------------------



(Changed your code a bit to make my explanation clearer.)

If you do that, you're saying that you want method() to execute even if #1 fails. That's not generally a good idea. The only way that makes any sense is if, when #1 throws an exception, you are able to recover from it and do its job in some other fashion, (such as falling back on some file in a default location you know will always be available if you're unable to open a file at a preferred location), or if what it's doing is something "extra" that you can live without. If this is the case, you can split it up, as Jesper shows.

However, I fear you may be suffering from a common misconception that exceptions are just just a natural occurrence and try/catch was put in the language to get them out of your way, or that catch somehow fixes the error. Don't fall into that trap. The exception mechanism exists so that you can separate your "happy path" code from your error handling code. They don't fix any errors though. When something goes wrong, you have to either fix it (by catching and actually handling the exception, or you have to let the caller know that it went wrong--that you were unable to to what he asked you to do (by not catching in the first place, or by rethrowing).
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One interpretation of the original question is that any exceptions thrown by method() should be ignored by the exception handling. That only makes sense to me if there are specific exceptions that the rest of the code could throw that you can handle here, but additional exceptions (such as those thrown by method() - which would suggest a more serious error) need to be thrown up to the level above.

If that is what Vis means, then I think the real problem is using a catch (Exception). If there are specific things that can be handled, these should be throwing specific exceptions. Then those specific exceptions should be caught here, rather than catching everything. Any other exceptions would be thrown to the level above.

(Let me know if that doesn't make sense!)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:
If that is what Vis means, then I think the real problem is using a catch (Exception).



Yeah, that's almost always a bad idea.
 
kul karni
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you everybody for the suggestions. Appreciated.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic