This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Question about an exception  RSS feed

 
Greenhorn
Posts: 28
  • 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
Greenhorn
Posts: 28
  • 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?
 
Bartender
Posts: 9569
189
  • 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
Greenhorn
Posts: 28
  • 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: 39084
714
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!
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!