Forums Register Login

exception handling

+Pie Number of slices to send: Send
I am confused about the flow of the exception statements.When I run this I get try and 3.I thought it should print Hello also.
If I include a code return 5 in the catch block the compiler shows an error saying System.out.println("Hello"); not reached.If it is because of the return 5; then why I am not getting this error with only the below code as if there is no exception the code after the finally block should be executed?Can anyone please help.
public class Test5 {
public static void main (String args []) {
System.out.println(A());
}
public static int A(){
try{
int i,j,k;
i=4;j=2;
k=i/j;
System.out.println("try");
return 3;
}
catch(ArithmeticException e){}
finally{}
System.out.println("Hello");
return 4;
}
}
+Pie Number of slices to send: Send
Your System.out.println("Hello") is not in the finally block. I think it's typo. It should be
Finally {
System.out.println("Hello")
return 4;
}
+Pie Number of slices to send: Send
Mindy,
Let's reformat your code so it's more readable.

1. In your current example, you shouldn't see "Hello" as part of your output because of the "return 3;" statement. Once you return from your method, it will not continue processing any other statements in the your method. However, if you put the System.out.println("Hello") inside the finally block then you would have seen "Hello" in the output, since anything inside a finally block gets executed before returning from a method.
2. If you put a "return 5;" inside the "catch(ArithmeticException e) {}" block, then the compiler will complain about the System.out.println() statement. Think about it. If there are no exceptions, then return 3 gets executed and you don't execute the System.out line. Now if an exception does get thrown and you fall into the catch block, then you'll execute return 5 - again the System.out line doesn't get executed. Whatever happens inside the try block (exception or no exception), you'll never reach the System.out.println("Hello") statement.
NOTE, the compiler must be pretty smart to figure out that ArithmeticException is the only exception that can be thrown inside your try block. I would figure if some other exception where thrown, then you could reach the System.out.println("Hello") statement before returning with an uncaught exception. If it's a checked exception, then you must specify it in the method signature.
-Peter

[This message has been edited by Peter Tran (edited February 05, 2001).]
+Pie Number of slices to send: Send
Yes peter my question is why the copiler doesnt complain System.out.println("Hello"); not reached with the above code (as you said its not reached in anycase becoz of the return statement).But as soon as I put return 5 in the catch block it does so.I think we should get the error in both cases right?
+Pie Number of slices to send: Send
Mindy,
No, you will not get a compile time error for both cases. You won't know if you'll get an ArithmeticException until runtime. The compiler doesn't know what will happen inside your try block until runtime. If an ArithmeticException does get thrown, then it will be caught. Since nothing gets done inside the catch block, execution will continue and you will see "Hello 4" as your output. Change your "j" variable to int j = 0; and you'll see this happening. Since it is possible to reach the last two statements in your method, the compiler will not complain. However, it will complain if you add a return inside your catch block, because it won't be able to reach the last two statement in the your method.
-Peter
[This message has been edited by Peter Tran (edited February 05, 2001).]
+Pie Number of slices to send: Send
thanks peter for the help.Will post more doubts
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 877 times.
Similar Threads
Exceptions
COnfusion About Unreachable Code
doubts???????
Enthuware questions
Why is 5 getting printed here?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:44:46.