Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Exceptions question

 
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?
 
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
 
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
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic