File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

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!!!


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.


"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: 918

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.


I agree. Here's the link:
subject: About catching Exception instead of more specific exception.
It's not a secret anymore!