• 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

SCJP 1.4 K&B Book Chp 4 clarification reqd.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Under Certification summary in SCJP 1.4 K&B Book Chp 4 the following is given :"A finally block is not required, but if there is one it must follow the catch. It is guaranteed to be called except in the special cases where the try or catch code raises an uncaught exception or issues a System.exit()."

In the above can somebody explain how is the statement "the finally block is guaranteed to be called except in the special cases where the try or catch code raises an uncaught exception " is correct? Im clear about the System.exit().

Thanks
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepthi,

finally block will be called after executing a try-catch block, whether an exception has ocurred or not, and whether the exception is being caught or not.

Except in cases of System.out.exit() and jvm shutdown, finally block will be executed. finally block is mainly used to cleanup the resources.

Mamatha
SCJP(1.4)
 
j deepthi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mamatha. So you agree with me that it is a faulty statement in the book.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepti,

I don't think it's a faulty statement because it says except in the special cases.......the finally block is guaranteed to be called.

But I have another question about this sentence, it says "A finally block is not required, but if there is one it must follow the catch."

But what I understand from k&b SCJP 5 book is that either catch or finally block is required. So there could be a finally block that may follow a try block and not necessarily a catch block.

Or is this a change in Java 5 from Java 1.4? Sorry I have not seen the k&b SCJP 1.4 book.

Could somebody pls clarify?
 
Mamatha Preetham
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kiran,

I understand your ambiguity..
For your first question:
"A finally block is not required, but if there is one it must follow the catch."

A finally block is mainly used to clean up the code, close your files..
If you have a try-catch block, where an exception occurs and it is handled in the catch block, then finally must follow the catch block....

Then for the second question:
"either catch or finally block is required. So there could be a finally block that may follow a try block and not necessarily a catch block."

You dont need a catch clause, when the exception is going to be passed back to the calling method, that time, a try block will be followed by a finally block...

Likewise, you dont need a finally clause, if you have no resources to cleanup after the try block completes, that time, a try block will be followed by catch block...

Cheers,
Mamatha
SCJP(1.4)
 
reply
    Bookmark Topic Watch Topic
  • New Topic