| Author |
why exception is not thrown
|
saikrishna cinux
Ranch Hand
Joined: Apr 16, 2005
Posts: 689
|
|
hi here i am dividing 10 by 0 but it is not displaying any exception in the runtime just displaying peace? can any one explain me the reason
|
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
|
 |
Kj Reddy
Ranch Hand
Joined: Sep 20, 2003
Posts: 1697
|
|
Saikrishna, The following line throws java.lang.ArithmeticException: / by zero int i = 4541 / 0 ; But you are not catching the exception so it just comes to finally block. Try the following:
|
 |
Gowher Naik
Ranch Hand
Joined: Feb 07, 2005
Posts: 643
|
|
in your code when exception occurs at line int i = 4541 / 0 the control jumps to finally block this is why exception is not thrown. As you know finally block always executes and When exception is thrown at line i = 4541 / 0 and you donot handle it line System.out.println(i) will never execute and then finaly block is executed. check code below which throws Exception
|
 |
Chetan Raju
Ranch Hand
Joined: Aug 02, 2006
Posts: 109
|
|
In your case, an Arithmetic exception is thrown in a catch clause. Then there is a finally clause which will be executed. Since finally has a return statement, this overrides any exception. That means the expection that was raised in catch block is lost after the return statement.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Chetan Raju: In your case, an Arithmetic exception is thrown in a catch clause. Then there is a finally clause which will be executed. Since finally has a return statement, this overrides any exception. That means the expection that was raised in catch block is lost after the return statement.
Yes, the JLS would say that this is is a case of the finally block "ending abruptly" and that the original reason is lost.
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Ramamoorthy Periasamy
Ranch Hand
Joined: Feb 06, 2006
Posts: 30
|
|
Yes. When you use return or throw in finally block it will show a warning message called "finally block does not complete normally". Always remember before printing the RuntimeException it executes the finally block (if anything defined). In this case since we had the return statement in finally compiler didnt get a chance to print the RuntimeException!
|
 |
 |
|
|
subject: why exception is not thrown
|
|
|