• 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Question about an exception  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

a finally block may throw an exception and if so, any exception thrown by the try or catch block is lost.



Is this true? So, compiler thinks tc.m2() may not throw any RuntimeException, therefore it decides exception inside catch block will propagate and give a compile error.
But if throw a NPE instead of tc.m2() in catch block, compiler understands it. And according to the rule above, all other exceptions are lost but only exception in finally block will throw.
Am I right?
 
Micheal Bush
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does compiler still give an error if m2 throws Exception in the method signature ? Compiler will determine that tc.m2() never compile normally, right?
 
Saloon Keeper
Posts: 9856
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can very easily check this by writing the code and compiling it.
 
Micheal Bush
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You can very easily check this by writing the code and compiling it.




Thanks for your comment but I need a concrete answer regarding my question. Output from IDE will not tell me there is a rule.
 
author & internet detective
Posts: 39159
724
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Micheal Bush wrote:

a finally block may throw an exception and if so, any exception thrown by the try or catch block is lost.



Is this true? So, compiler thinks tc.m2() may not throw any RuntimeException, therefore it decides exception inside catch block will propagate and give a compile error.
But if throw a NPE instead of tc.m2() in catch block, compiler understands it. And according to the rule above, all other exceptions are lost but only exception in finally block will throw.
Am I right?


Yes, it's true if m2() throws an exception. Remember that the rule about exceptions being lost is happening at runtime, not compile time. This is why you can have different behavior if an exception is (or isn't) thrown!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!