Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Goes Straight to Finally Block after Skipping Catch Block  RSS feed

 
Greenhorn
Posts: 2
  • 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 Hand
Posts: 3179
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this program instead, to see what it does:
 
Marshal
Posts: 63843
209
  • 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: 250
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: 63843
209
  • 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: 250
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell!
 
Campbell Ritchie
Marshal
Posts: 63843
209
  • 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.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!