wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why Exact Exception ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why Exact Exception ?" Watch "Why Exact Exception ?" New topic
Author

Why Exact Exception ?

Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi,
It always said in the K&B book that we should catch the exact exception, I always follow the same rule while doing coding. But I have one question, Why do we need to follow this adage, as i can do my work without catching the proper exception for eg: look at the following code :


Here while catching the exception whether i use Exception e or NullPointerException e, I am getting the same result. there will be some reason thats why this rule was mentioned in K$B, Can some one help me to explain that?


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39570
    
  27
Because the code in the try block might throw other exceptions as well, and you may want to differentiate between them. Some exceptions may be expected and not actually cause a problem if handled properly (e.g., NumberFormatException), while others needs to be logged, or handled in a different way.

Some exceptions may also have more information and methods you can use in the catch block; that would be lost if you just catch Exception.


Ping & DNS - updated with new look and Ping home screen widget
Hiral Amodia
Greenhorn

Joined: May 14, 2008
Posts: 2
Ya most of the books which explan exception handling does say to catch proper exceptions. This is a way to make a person habituated of catching proper exception from the beginning.
The code and case which you have mentioned are very general. The action which you have taken on catching an exception is also very general. This is NOT the scenario in real world JAVA coding. Many times you come across situations where you have to take some specific action on catching a specific exception and thats the reason why all the experts mention to catch the proper exception from the beginning.
Let me know if I have been able to answer the query.

Thanks and Regards,
Hiral
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi Ulf,
This is what has been written in Book or Java, Can you please give me a small example of your explanation? its a Request.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

that means when we just have to logged the exception and coming out of the method, then i can just catch the exception and print out the stack log and exit from the program. How can i use the specific exception in constructive point of view ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39570
    
  27
An example are the methods in the java.io.File class. Some of those throw either IOException (if the method can't proceed because of some genuine I/O problem) or SecurityException (if the method is not allowed to proceed because a SecurityManager prevents it). Those are very different error conditions that can (and often need to) be treated differently.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39570
    
  27
Just saw your last post - in long-running applications (e.g. web apps), quitting the app is not an option. So you need to handle exceptions, and recover from them in a meaningful way that allows the app to continue working.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Ok Ulf,

So Can I say that, when we have to take some action after catching the exception, We should catch the specific Exception and if we just want to re throw the exception after catching, then no need to catch the specific exception. what will you suggest Ulf, please guide.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39570
    
  27
There are no hard and fast rules. Sometimes you catch and handle exceptions, sometimes it's better to keep a "throws" clause in the method signature, and sometimes you may do both (catch it and re-throw it).

What I wouldn't do is "catch Exception", and then re-throw it if it actually is a more specific exception that you could be catching instead. That may come back to bite you later if you add more code in the try block that throws a different exception that should be handled differently.

Sometimes it's OK to be lazy (and just catch Exception), sometimes not. If in doubt, be specific.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Thanks, Ulf,
One last thing I would like to know , What is the moral of story from your point of view, should i always catch the specific exception ? Because this practice will be helpful always or catch exception depending upon the situation ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39570
    
  27
As I said, there are no absolute rules. As a practice, I strive to be specific, and would encourage others to do the same.

As a side note, I like to run FindBugs over any sizeable codebase I work with, and catching Exception when a more specific exception is thrown is one of the things it will flag as an issue. So in order to get a short FindBugs trouble reports this practice needs to be followed.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Thanks, Ulf. This will helps me a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Exact Exception ?
 
Similar Threads
Exception problem.
Exception
"Double" and "double" - what's the difference?
Sax Exception conversion to user defined exception not working.
Exception declaration