Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try without catch? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "try without catch?" Watch "try without catch?" New topic
Author

try without catch?

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Under what circumstances would we use a try block
without a catch block?
Would a finally block be required in this situation?
Thanks!
Drew
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
Yes , the try block has to followed by atleast a catch ( more than one if required ) OR a
finally block.
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
OK, but when would you actually use a try/finally without a catch?
try{
// code that might throw an exception
}
finally{
// do something here
}
If the code in the try block throws a checked exception wouldn't it require the use of a catch block anyway?
Regards,
Drew
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Drew!
I'll try to illustrate this w/ a lil snippet:

So we can delegate error handling to higher scope, but yet we can do some things in finally blocks. Snippet has the idea but isnt very good example for this.
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java™ 2 Platform
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
You can use try/finally without a catch block. Usually, you just want to release some non-memory resources allocated in the try block, and you want to make sure that these resources will be released no matter what. You use try/finally block to accomplish that.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Helo Cameron!
That was much better example.
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java™ 2 Platform
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
I just want to add one more thing, if there is no catch block or specific or related catch block (of super class of the exception raised) then the Exception is said to be uncaught and method terminate after final block.
--Farooq
[This message has been edited by Muhammad Farooq (edited August 27, 2001).]


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
 
wood burning stoves
 
subject: try without catch?
 
Similar Threads
'catch' without 'try'
try block
not able to understand the output of the following piece of code ?
help in exceptions
empty try block