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

Exceptions: About finally

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following Q is from RHE chap 5. I am not sure that I get these are points right? Please help me! Thanks!
1. finally is not a "final catch" or is not a kind of "catch(...)" in c++. finally is just a simple "printf" statement? If finally is not a "finally catch", why need it?
2. if an exception is not catched, The excecution exits the method IMMEDIATLY? True or False?
try{
URL u=new URL(path);//assume path is a previously defined String
Object obj=in.readObject();// in is a valid ObjectInputStream
System.out.println("Success");
}
catch(MalformedURLException e){
System.out.println("Bad URL");
}
catch(StreamCorruptedException e){
System.out.println("Bad File Contents");
}
catch(Exception e){
System.out.println("General Exception");
}
finally{
System.out.println("Finally Part");
}
System.out.println("Carrying On");
What lines are output if the code at the line 3 throws a OutOfMemoryError? Choose all valid answers.
a. Success
b. Bad URL
c. Bad File Contents
d. General Exception
e. Finally part
f. Carrying On
Ans: e
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally is not a "catch" statement, as it does not check explicity for exceptions that are thrown. You can think of "finally" as a default statement/code block that is sure to run whether an exception is thrown/caught or not. Programmers generally use the "finally" statement to be sure of clearing/closing allocated resources that could be corrupted when an exception is thrown. The only time "finally" is not executed is when the Thread calling the method enclosed in the try block dies for some reason.
Hope this helps.
Alan
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic