purpose of exception handling 'finally' clause

Thread Solved

Join Date: Oct 2004
Posts: 5
Reputation: javaLearner is an unknown quantity at this point 
Solved Threads: 0
javaLearner's Avatar
javaLearner javaLearner is offline Offline
Newbie Poster

purpose of exception handling 'finally' clause

 
0
  #1
Oct 24th, 2004
hi.

more or less a stupid question, but anyway..

why have a 'finally' statement in java exception handling -why not just clean up in the 'catch' clause??

is it for when an exception cannot be caught?? if not, then 'finally' seems a little redundant to me..

cheers.

:eek:
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: purpose of exception handling 'finally' clause

 
0
  #2
Oct 25th, 2004
The finally clause executes wheter or not an exception is thrown. Except when a catch clause invokes System.exit() of course!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: purpose of exception handling 'finally' clause

 
0
  #3
Oct 25th, 2004
Taken from the Java in a Nutshell

try {
// Normally this code runs from the top of the block to the bottom
// without problems. But it can sometimes throw and exception,
// either directly or with a throw statement or intdirectly by calling
// a method that throws and exception.
}
catch (SomeException e1) {
// This block contains statements that handle the exception object
// of type SomeException or a subclass of that type. Statements in
// this block can refer to that exception object by the name e1.
}
catch (AnotherException e2) {
// This block contains statements that handle the exception object
// of type AnotherException or a subclass of that type. Statements in
// this block can refer to that exception object by the name e2.
}
finally {
// This block contains statements that are ALWAYS executed
// after leaving the try clause, regardless of whether we leave it:
// 1) normally after reaching the bottom of the block;
// 2) because of a break, continue, or return statement;
// 3) with an exception handled by a catch clause above; or
// 4) with an uncaught exception that has not been handled.
// If the try clause calls System.exit(), however, the interpreter
// exits before the finally clause can be run.
}

Also, later on in the finally description of the document:

If control leaves the try block because of a return, continue, or break statement, the finally block is executed before control transfers to its new destination.

This would also apply to if it is used in the catch block.

Hope this clarifies things for you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 5
Reputation: javaLearner is an unknown quantity at this point 
Solved Threads: 0
javaLearner's Avatar
javaLearner javaLearner is offline Offline
Newbie Poster

Re: purpose of exception handling 'finally' clause

 
0
  #4
Oct 27th, 2004
Jerbo and Server_Crash,

that's great -thanks for your help.

It seems Java in a Nutshell is a good resource to have..

Cheers,
Less-confused ..
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC