Forums Register Login

exception

+Pie Number of slices to send: Send
hi

why we can;t throw more than one excption in a try block as shown in below code.

public int method6() {

//throw new RuntimeException();
try{
throw new ArithmeticException();
throw new RuntimeException();
}
catch(RuntimeException re){
}
return 0;
}

but if we provide in the if else code then it will compiled
public int method6() throws Exception {

try{
int i = 1;
if (i==1){
throw new ArithmeticException(); }
else {

throw new RuntimeException();}
}
catch(RuntimeException re){
}
return 0;

}
+Pie Number of slices to send: Send
In your first example the second throw clause will never be reached, so the compiler will complain about it being unreachable.

However, in the second example, there is a chance of either one of the throw clauses will execute (depends on the result of the expression in the if-stat), so the compiler won�t complain about it.
+Pie Number of slices to send: Send
thanks a lot for your reply, it is make sense for me now.
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 753 times.
Similar Threads
Why it does not cimpile without finally ?
can a static block through exception?
A runtime exception is throws in the catch clause:
exception handling
Exceptions
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:06:12.