| Author |
Exceptions question
|
AJ Brown
Greenhorn
Joined: Mar 30, 2004
Posts: 17
|
|
I'm referring to questions 14 and 15 on page 280-281 in Kathy and Bert's SCJP book. Why does Q.14 print out that a RuntimeException has occurred while Q.15 does not? Is it because Q.14 has no catch statement?
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
AJ, Not everyone here has that book. If you'd like a response from any of those people (myself included), you'll have to copy the question here for us to see. Corey
|
SCJP Tipline, etc.
|
 |
AJ Brown
Greenhorn
Joined: Mar 30, 2004
Posts: 17
|
|
Question 14 public class MyProgram { public static void throwit() { throw new RuntimeException(); } public static void main (String args[]){ try { System.out.println("Hello world "); throwit(); System.out.println("Done with try block"); } finally{ System.out.println("Finally executing"); } } } answer is "the program will print Hello world, then will print Finally executing, then will print that a RuntimeException had occurred. Question 15 public class RTExcept { public static void throwit() { System.out.print("throwit "); throw new RuntimeException(); } public static void main(String [] args) { try { System.out.print("hello "); throwit(); } catch (Exception re) { System.out.print("caught "); } finally{ System.out.print("finally "); } System.out.println("after "); } } answer is hello throwit caught finally after
|
 |
pallavi utukuri
Ranch Hand
Joined: Feb 10, 2004
Posts: 182
|
|
|
ya it is due to lack of catch block only....in the second example a catch block is provided soo the RunTimeException which is a subclass of Exception is caught and the normal flow of program continues after finally
|
Thanks,<br />Pallavi
|
 |
 |
|
|
subject: Exceptions question
|
|
|