944,176 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 9923
  • Java RSS
Oct 24th, 2004
0

purpose of exception handling 'finally' clause

Expand Post »
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:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javaLearner is offline Offline
5 posts
since Oct 2004
Oct 25th, 2004
0

Re: purpose of exception handling 'finally' clause

The finally clause executes wheter or not an exception is thrown. Except when a catch clause invokes System.exit() of course!
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Oct 25th, 2004
0

Re: purpose of exception handling 'finally' clause

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.
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Oct 27th, 2004
0

Re: purpose of exception handling 'finally' clause

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 ..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javaLearner is offline Offline
5 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how do i display updated records from database
Next Thread in Java Forum Timeline: help with regex...and marking up text in JTextpane with html





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC