Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions question

 
AJ Brown
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
AJ Brown
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic