• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Omitting the catch block

 
Ranch Hand
Posts: 388
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a quick question if any of you don't mind......!

What's the point of having no catch block when handling an exception? For example, having a try block paired with just a finally block.

You use the try block to run risky code - but without a catch block the compiler won't allow it anyway - doesn't that make the 'try' block a bit pointless? (other than being required!)

The only example i could think of is to allow you to mix and match the benefits of ducking an exception and running code in the finally block. Does that sound about right?

Thanks,

Nick
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nick woodward wrote:The only example i could think of is to allow you to mix and match the benefits of ducking an exception and running code in the finally block. Does that sound about right?


Pretty much, although I'm not sure that "ducking" is quite the right term. If you don't specify a catch clause, whatever exception that would have been thrown will still be thrown, but the thing about a finally clause is that it will be executed even if an exception occurs.

HIH

Winston
 
nick woodward
Ranch Hand
Posts: 388
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, sorry, i've just heard the word used before to describe not handling the exception in that specific method and allowing it to be passed up (down?) the stack.

anyway, thanks for the quick reply!

Nick
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nick woodward wrote:The only example i could think of is to allow you to mix and match the benefits of ducking an exception and running code in the finally block. Does that sound about right?


That sounds about right, although there are other examples as well

Probably an obvious is described in the following code snippet (prior to Java 7)If something went wrong while reading the file to create the list of lines in the readLines method, you don't want to process the lines because that's pretty useless. So the IOException is thrown from the readLines method and handled in the calculate method. But you definitely want to close all resources you have used. When using try-with-resources (Java 7 and later) the readLines method is more concise but the code is equivalent (the compiler will generate the appropriate finally block for you)

Another example is when you are working with concurrency and locks. When you acquire a lock, you always want to release this lock once you are done. So all code that needs to be executed while the lock is held, will be executed inside a try-finally block and releasing the lock happens in the finally block. This way you are ensured the lock is always released when necessaryThis idiom is even documented in the API documentation.

Hope it helps!
Kind regards,
Roel

Disclaimer: none of these topics (try-with-resources, file I/O, concurrency, and locks) are not a topic of the OCA certification exams. I used them for demonstration purposes only. So if you don't understand what's happening, don't worry and just move on! All these topics are an OCP exam objective.

[edit] Forgot to mention that try-with-resources and file I/O are also not on the OCA exam. Thanks Jeanne!
 
author & internet detective
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading a file isn't on the OCA either. Nor is any other use case I can think of where not having a catch block is wise.

Because of this, the exceptions questions you tend to see on the OCA exam tend to be contrived.
 
Ranch Hand
Posts: 251
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Roel De Nijs
 
nick woodward
Ranch Hand
Posts: 388
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it does indeed help Roel, thanks a lot!

Nick
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Another example is when you are working with concurrency and locks...


Yeah! That's the example I was trying to remember. Have a cow for solving my Alzheimers.

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic