This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Q on Exception Handling

 
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:


What is the result?
A. test end
B. Compilation fails.
C. test runtime end
D. test exception end
E. A Throwable is thrown by main at runtime.
Answer: D

Why doestn't the Catch block in Main() execute?
and how come System.out.print(�end �); executed?
 
pioneer
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priti Manas,

The exception which is thrown on line 14, is a RuntimeException which extends Exception. This exception is caught at line 16 and handled. Once handled we are safe from exception. So the exception is not thrown out of test method and need not be handled at line 20. In fact exception does not come out of test method at all, and since we have proper precautions at line 20 , we are good to execute line 21.

Assume, the exception thrown at line 14 is not caught within method test() and is thrown back to main method, and even there it is not properly handled then line 21 throws compiler error, saying "Unreachable code".
 
pioneer
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by priti manas duddilla:
Why doestn't the Catch block in Main() execute?
and how come System.out.print(�end �); executed?



Hi Priti,

The main() catch block doesn't execute because the exception is not re-thrown from the test() method. The catch() block in the test() method catches the exception thrown by the test() method. If you want the method that calls test() to react to exceptions, you need to throw it again. You can do this in a few different ways.

1: you can add a line inside the catch block at line 16 to throw the exception again. (Add it after the System.out.print() line.)

2: you can add a finally block after line 16 with a throw in it. In this case, the finally() block will be called from the catch() block at 16, *NOT* the try() block at 12.

3: you can add a throw outside of the try/catch block between lines 16 and 17. Execution of the method doesn't stop with the catch() block unless you return or exit within it.

Hope this helps...
Aloha,
Doug
 
priti manas duddilla
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh thats nice explanation over there!

But is it the case with checked exceptions too?
I think they propagate no matter what, even after being handled right?

Please clarify.
 
Author
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No - once a Throwable (of which Exception is a subclass) is caught by a catch block, it will *never* propagate unless it is explicitly rethrown from the catch block.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!