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; } }
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).]
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?
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).]