• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Question on exception throwing

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What letters are written to the standard output with the following code?
1. class Unchecked {
2. public static void main(String args[]) {
3. try {
4. method();
5. } catch(Exception e) { }
6. }
7. static void method() {
8. try {
9. wrench();
10. System.out.println("a");
11. } catch(ArithmeticException e) {
12. System.out.println("b");
13. } finally {
14. System.out.println("c");
15. }
16. System.out.println("d");
17. }
18. static void wrench() {
19. throw new NullPointerException();
20. }
21. }
22. Select all valid answers.

The answer provided is 'c'. Is that correct ?

Ans :
a. "a"
b. "b"
c. "c"
d. "d"
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes this is correct. Follow what's going on here: method() is called, which in turn, calls wrench(), which throws a NullPointerException, the jvm goes up the stack searching for the appropriate handler. The first one it comes across is Exception e which is handled by method() itself. Notice that this is the only exception that doesn't give any output. Next, the finally statement is executed which in turn prints out c. Note that the finally statement is always executed unless a call to System.exit() has been called. That means when an execption is thrown, and when one isn't thrown, finally still executes.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sean,
acc to u Exception is to be handled first before finally is executed then why is the output showing like this
c
caught
if the code had been
try
{
method()
}
catch(Exception e)
{
System.out.println("caught");
}
and what is the diff it makes if a RuntimeException is thrown,does it essentially need to be caught??
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I confused that one a bit. What happens is, that if there are no appropriate handlers, then finally is executed. And if it still isn't handled, then it goes to the calling method as it does in this case. Now if you modified your code and had it catch (Exception e) where it says (ArithmeticException e) then finally would execute after, like i said earlier. I'm sorry I screwed that one up a bit. I think I answered too many posts too fast without sitting back and thinking about it.
[This message has been edited by Sean Casey (edited April 19, 2001).]
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic