This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

why doesn't the finally block run in a try-catch?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A finally block in a try-catch is always supposed to run (except if code from the try or catch blocks call System.exit()).
So I am wondering why the finally block doesn't run in the following code:



The message that I get is: "exception MyException is never thrown in body of corresponding try statement".
Ok, that is true, but I thought that finally block ALWAYS runs, except when System.exit() is called.
Can someone explain this to me?

Thanks!
 
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roger Jenkins wrote:
The message that I get is: "exception MyException is never thrown in body of corresponding try statement".
Ok, that is true, but I thought that finally block ALWAYS runs, except when System.exit() is called.
Can someone explain this to me?



Are you running code with compile errors (using an IDE that supports it, of course)?  If so, then read this...

https://coderanch.com/wiki/660183/Fix-Compiler-Errors-Running-Application

Henry
 
Master Rancher
Posts: 4706
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry suggests, that code doesn't compile, and that message is thrown by the IDE when it attempts to run code that doesn't compile.
 
Roger Jenkins
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Roger Jenkins wrote:
The message that I get is: "exception MyException is never thrown in body of corresponding try statement".
Ok, that is true, but I thought that finally block ALWAYS runs, except when System.exit() is called.
Can someone explain this to me?



Are you running code with compile errors (using an IDE that supports it, of course)?  If so, then read this...

https://coderanch.com/wiki/660183/Fix-Compiler-Errors-Running-Application

Henry



Thank you for your answer and for the link!

Yes, I was running code with compile errors and an IDE that supports it.
So the first take-away message for me is that I should fix all compiler errors before running the application: the fact that "exception MyException is never thrown in body of corresponding try statement" is a compiler error so that should be fixed first, before I start thinking about if the finally block always runs or not.
And the second take-away message is that an IDE helps confuse these issue's so it is preferred to start learning Java without an IDE.
I am assuming that I am correct in my take-away messages unless you want to add something.

Thanks again!
 
Roger Jenkins
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:As Henry suggests, that code doesn't compile, and that message is thrown by the IDE when it attempts to run code that doesn't compile.



Thanks for your answer!
 
reply
    Bookmark Topic Watch Topic
  • New Topic