Exception Handling

I'm writing a method that throws 7-8 exceptions. Is it a bad practice to have multiple exceptions caught in a try - catch block? Also, what is the difference between catching the exception within your method and delcaring that the method throws the exception? Thanks.
[275 byte] By [snguyena] at [2007-11-27 8:17:31]
# 1

> I'm writing a method that throws 7-8 exceptions. Is

> it a bad practice to have multiple exceptions caught

> in a try - catch block? Also, what is the difference

> between catching the exception within your method and

> delcaring that the method throws the exception?

> Thanks.

It is not bad practice to catch multiple exceptions. It all depends on where you can best deal with the exception. If the method where the exception is thrown is the most appropriate place to deal with it, then catch the exception there and deal with the exception. If it's no business of that method to deal with the exception, then declare that that method could throw the exception.

The difference between catching an exception and declaring the method to throw the exception is where you have to actually place your exception catch. If you declare the method to throw the exception then it is the method that calls the method that has to deal with catching the exception,

Make sense?

bryanoa at 2007-7-12 20:02:42 > top of Java-index,Java Essentials,New To Java...
# 2
If you really don't care about how to handle the different exceptions (or if your lazy) you can always do a general catch(Exception e) to handle all of the exceptions.
jwarzecha at 2007-7-12 20:02:42 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks for replying.I kind of get it.Is this a correct? If I want to know trace the exception back to that method I should use the try - catch within the method. If not I don't care then I can include it in the method declaration.
snguyena at 2007-7-12 20:02:42 > top of Java-index,Java Essentials,New To Java...
# 4

If your method to handle the exception--for instance, retrying or providing some default value--then you should catch it and handle it.

If your method is not in a position to actually do something about the exception, then you should just declare that you throw it. Sometimes, though, even if you can't handle the exception, you might want to wrap it in one that's more appropriate for your layer and throw that.

jverda at 2007-7-12 20:02:42 > top of Java-index,Java Essentials,New To Java...
# 5

> If your method to handle the exception--for instance,

> retrying or providing some default value--then you

> should catch it and handle it.

>

> If your method is not in a position to actually do

> something about the exception, then you should just

> declare that you throw it. Sometimes, though, even if

> you can't handle the exception, you might want to

> wrap it in one that's more appropriate for your layer

> and throw that.

or wrap these exception into throwing a runtimeexception. then all those methods that sont care about dont have to catch or rethrow it.

kilyasa at 2007-7-12 20:02:42 > top of Java-index,Java Essentials,New To Java...
# 6

> > If your method to handle the exception--for

> instance,

> > retrying or providing some default value--then you

> > should catch it and handle it.

> >

> > If your method is not in a position to actually do

> > something about the exception, then you should

> just

> > declare that you throw it. Sometimes, though, even

> if

> > you can't handle the exception, you might want to

> > wrap it in one that's more appropriate for your

> layer

> > and throw that.

>

>

>

> or wrap these exception into throwing a

> runtimeexception. then all those methods that sont

> care about dont have to catch or rethrow it.

ICKY BAD!!!

jverda at 2007-7-12 20:02:42 > top of Java-index,Java Essentials,New To Java...