Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

why exception is not thrown

 
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
 
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:
 
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
 
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.
 
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.
 
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic