• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Goes Straight to Finally Block after Skipping Catch Block

 
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?

 
Master Rancher
Posts: 3537
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this program instead, to see what it does:
 
Marshal
Posts: 69418
276
  • 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: 311
6
  • 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: 69418
276
  • 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: 311
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell!
 
Campbell Ritchie
Marshal
Posts: 69418
276
  • 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.
 
Those are the largest trousers in the world! Especially when next to this ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic