Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why exception is not thrown

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramamoorthy Periasamy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic