• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Catching Exceptions

 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys

I'm finishing the study guide, and the last chapter is about Exceptions. It is a really peaceful chapter, there is not a lot of things to know about Exceptions for the OCA Exam.
Although it is one of the easiest chapters of the book, there is something that I've read and never paid attention before. It is about throwing an exception in the finally block.
I know that the finally block will always be executed (ok, not always, there is an exception for this rule, right?). What I mean is, the finally will be executed if the try block executes perfectly as well as if it throws an exception that is caught by the catch, even if there is a return statement inside the catch block (the finally is executed and then the return).
That's ok, but let's take a look at the following code:

When executing the previous code, the exception is thrown following the stack with the methods, starting from doSomething(), then start() and then main(). Ok, but where I marked with "1", the try block throws the Exception, and then the catch block catches this exception. But the catch block throws an exception too, and then the finally block executes and throws another exception, and the result is that the exception thrown by the finally is considered, while the exception thrown by the catch block is forgotten.
So, I understand that the last thrown exception is always the one that is considered.
The book says the following: "This is why you often see another try/catch inside a finally block - to make sure it doesn't mask the exception from the catch block". I didn't understand this sentence. What "mask the exception from the catch block" means? Could anyone give me an example?

Thank you.
 
author & internet detective
Posts: 42102
933
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prior to Java 7, you'd see database code that had something like this in it:



The problem was that if rs.close() threw an exception, you still wanted the stmt and conn to be closed. Thanks to new features in Java 7, this ugly code is pretty much a thing of the past.

For the exam, remember that System.exit() doesn't exist so finally always runs.

Also, things get more complicated with exceptions when you get to the OCP.
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jeanne, thanks for the answer

Regarding the sentence "This is why you often see another try/catch inside a finally block - to make sure it doesn't mask the exception from the catch block", the "doesn't mask the exception from the catch block" part is refering to try blocks inside a finally that are used to not mask the exception thrown by the catch, is that right?
I didn't understand this, how the finally would mask the exception thrown by the catch block? Maybe I'm worried about something that is not included in the OCA exam, but I'm just curious.

Thanks
 
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example:

If the exception caused by cleaning up resources isn't caught inside the finally-clause, it will propagate up outside of the method call, instead of the original exception we're interested in fixing. Some say it doesn't really matter, especially considering try-with-resources doesn't change this situation much.
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it now! The exception is handled inside the finally with another try-catch, so the finally doesn't throw an exception, and the exception thrown by the try block will not be ignored.
So, when closing or cleaning resources inside the finally block, a possible exception is handled inside it, making it independent of the try, which means that the caller of this code wouldn't have to worry about what happens inside the finally.
I think that's it.

Thanks Stephan and Jeanne.
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic