• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions question

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic