• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Goes Straight to Finally Block after Skipping Catch Block  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this program skip the catch block and doesnt throw the second RuntimeException? Instead it goes to the finally block after the first RuntimeException is thrown. Shouldn't the catch block catch the first RuntimeException allowing the prgram to throw a new RuntimeException?

 
Ranch Foreman
Posts: 3243
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this program instead, to see what it does:
 
Marshal
Posts: 64483
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Timothy Giles wrote:. . . . Shouldn't the catch block catch the first RuntimeException allowing the prgram to throw a new RuntimeException? . . .

If you have followed MS' suggestion, you will know that is exactly what happens. But the finally is always executed whether an exception is thrown or not; the exception thrown in the finally takes priority over any exceptions thrown elsewhere.
That is why it is usually a bad idea to throw an exception or return a value from a finally; it overwrites the results of the rest of the method.
 
Ranch Hand
Posts: 257
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
That is why it is usually a bad idea to throw an exception or return a value from a finally; it overwrites the results of the rest of the method.


Campbell, Did you mean finally here?
 
Campbell Ritchie
Marshal
Posts: 64483
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles O'Leary wrote:. . . Campbell, Did you mean finally here?

Yes.
 
Charles O'Leary
Ranch Hand
Posts: 257
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell!
 
Campbell Ritchie
Marshal
Posts: 64483
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Timothy Giles wrote:Why does this program skip the catch block and doesnt throw the second RuntimeException? . . .

Actually it doesn't. Because you have an extraneous { and no ; at the end of line 2, it fails to compile.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!