• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Roger Jenkins
Ranch Hand
Posts: 31
  • 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!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • 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: 31
  • 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: 31
  • 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!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!