• 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

Why Exact Exception ?

 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Ulf. This will helps me a lot.
 
reply
    Bookmark Topic Watch Topic
  • New Topic