This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes About catching Exception  instead of more specific exception. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "About catching Exception  instead of more specific exception." Watch "About catching Exception  instead of more specific exception." New topic
Author

About catching Exception instead of more specific exception.

Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
Hello Everybody,

I am a little bit confused about exception handling. I have some question about it and I would very appreciate if you can help me!

1. If I have a method, that throws more than one exception. What is the right way to handle these exceptions? For example:
For Naming.lookup(�) there are three defined exception. What is the correct way to handle it? Catch every exception:


or catch only the main Exception:



2. Are there are some books, links with something like "best practice". Could you recommend me?

Thanks a lot for your help!!!

Regards,
Olena.


SCJP 1.4<br />SCJD 1.4 (in progress)
Wei-ju Wu
Ranch Hand

Joined: Feb 16, 2005
Posts: 147
Hi Olena,

there is a book that every Java developer should have which is "Effective Java" by Joshua Bloch. This also handles the topic of handling exceptions appropriately. In this case my guess would be that the topic "throw exceptions appropriate to the abstraction" would be a good start.

However, you need to regard the method where you handle these exceptions in the context of your application and decide which of the recommendations fits you best.

Wei-ju


"The UrlyBird catches the certificate. And he's gonna FlyByNight"<br /> <br />SCJP 1.2/5.0, SCJD, SCBCD, SCWCD, SCEA
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Olena,

In geneal is bad to catch the Exception exception because the Exception is the super(parent) class for all the exceptions runtime or not so you catch all and you can not make a distionction between the them.
But sometimes the runtime exceptions means a promming(logic) error and it better to let them to occur from beginig.

Mihai.


SCJP, SCJD, SCWCD, OCPJBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About catching Exception instead of more specific exception.