Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

A runtime exception is throws in the catch clause:

Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don’t understand this:
Both programs throw the Runtime-Exception „ArithmeticException“ in the catch clause.
But in Test81 the „finally“ clause prevents the ArithmeticException
From beeing thrown to the System level.
What happens?

C:\Java\EigeneJavaProgramme>java Test81

Why does it throw Arithmetic Exception NOW?

C:\Java\EigeneJavaProgramme>java Test81a
java.lang.ArithmeticException: / by zero
at Test81.main(
Exception in thread "main"
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact the ArithmeticException is thrown in both case. To see it, put a try catch statement at the place where you make your zero division.
The ouput give :
E:\Dev\src\Test>java Test81
Peace MAIN
In your Test81 class, the ArithmeticEception doesn't come up because it is not catched by any catch statement.
The finally clause is always executed no matter what happened in the catch clause.
But I don't known why in Test81 the program don't stop to notice the ArithmeticException.
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Test81, the return statement inside the finally {} essentially tells the JVM to forget about the exception that it was throwing, and just return "Peace" instead. This is why it's almost always a bad idea to do a return inside a finally. Also if any new exception were thrown inside the finally, only the new exception would be thrown - the old one would be forgotten. This may seem strange, but what other behavior would make more sense? It's not possible to throw two exceptions, or to throw one exception and also return a value - so the language designers decided to make the JVM do whatever the finally clause says, inpreference to what would have happened otherwise.
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic