aspose file tools*
The moose likes Beginning Java and the fly likes Can Java catch more than one exceptions from the single try_catch block? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Java catch more than one exceptions from the single try_catch block?" Watch "Can Java catch more than one exceptions from the single try_catch block?" New topic
Author

Can Java catch more than one exceptions from the single try_catch block?

Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
Can Java catch more than one exceptions from the single try-catch block? Suppose I have the following code:

try
{ method1();
method2();
method3();
}
catch(ArithmeticException ae)
{ }

And suppose method1(), method2(), method3() all throw (instead of *might* throw) ArithmeticException, Is Java able to catch all 3 exceptions?
I tried a code, it does not catch all 3 exceptions. Could only catch the exception thrown by the 1st method.
Please confirm me, thanks a lot.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
If method1()throws an exception, your program will immediately go to the catch block. method2() will not have a chance to throw anything.
-anthony
Sal Velinus
Greenhorn

Joined: Mar 05, 2002
Posts: 25
You could put method2 in a finally block and it will execute (or throw an exception). At least that's what I read earlier today.
smart andy
Greenhorn

Joined: Apr 08, 2002
Posts: 2
i agree.


(:... <img src="smile.gif" border="0">
Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
I suppose u will have to write 3 catch blocks for 3 diffrent expressions.
Otherwise,u write a catch blk with the father of
all exceptions as the argument.
catch (Exception e)
{
}
though i know
both ways it is not convincing answer enough.
thankz,
Chinmay


The strength of the Wolf is the pack & the strength of the pack is the wolf....Rudyard Kipling
Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
Ok
im sorry for that prev reply.
I did not read the question properly.
If all the 3 methods throw same exception then
the method which throws first will be caught and
control transferred to the catch blk.
JVM wont go on to evaluate the next method.
Sorry again.
I thought what if all the methods throw diff exceptions and u need to catch them.
.....Chinmay
 
Consider Paul's rocket mass heater.
 
subject: Can Java catch more than one exceptions from the single try_catch block?