473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

About FINALLY clause

This question is applicable to all the languages, but could someone provide
me with an code example where FINALLY is really necessary.
FINALLY is often used for this kind of code:

Try
....
open a file
read some date
.....
Catch
... dealing with the file error ....
Finally
close the file
End Try

But wouldn't the following (without FINALLY) work as well?

Try
....
open a file
read some date
.....
Catch
... dealing with the file error ....
End Try
close the file

Please tell me what I'm missing.
Thanks
Henri

Nov 19 '05 #1
4 1068
NO!

What happens if you can't "deal" with the file..say because the exception
was OutOfMemory (not what you thought it was going to be eh?) you should
still do what you can to close the file. Also, what happens in your
"dealing" with the exception that you decide to throw it...your close call
will never get called. The point of finally is that you are [almost]
guaranteed that the code will get called. Putting your cleanup AFTER the
try/catch doesn't provide a guarantee. Here's another case....your catch
code generates an exception...your cleanup will never get called.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Henri" <hm********@hotmail.com> wrote in message
news:uM*************@TK2MSFTNGP12.phx.gbl...
This question is applicable to all the languages, but could someone provide me with an code example where FINALLY is really necessary.
FINALLY is often used for this kind of code:

Try
....
open a file
read some date
....
Catch
... dealing with the file error ....
Finally
close the file
End Try

But wouldn't the following (without FINALLY) work as well?

Try
....
open a file
read some date
....
Catch
... dealing with the file error ....
End Try
close the file

Please tell me what I'm missing.
Thanks
Henri

Nov 19 '05 #2
Try
....
open a file
read some date
.....
Catch
... dealing with the file error ....
.... exit sub or rethrow error ...
Finally
close the file
End Try

In the above code the exit sub or rethrow error would normally exit the
function, and if an error occurs within the Catch block the function would
also be exited. Anything after the try block would not be executed.
However, anything within the Finally block will be executed before the
function is exited. That is why it is useful.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"Henri" <hm********@hotmail.com> wrote in message
news:uM*************@TK2MSFTNGP12.phx.gbl...
This question is applicable to all the languages, but could someone
provide
me with an code example where FINALLY is really necessary.
FINALLY is often used for this kind of code:

Try
....
open a file
read some date
....
Catch
... dealing with the file error ....
Finally
close the file
End Try

But wouldn't the following (without FINALLY) work as well?

Try
....
open a file
read some date
....
Catch
... dealing with the file error ....
End Try
close the file

Please tell me what I'm missing.
Thanks
Henri

Nov 19 '05 #3
I believe if you throw an exception from your catch clause, it will not
execute code outside of your try-catch clause if you throw an exception
up the stack. Any code you put inside of finally will run whether you
throw exceptions or not regardless.

anyone correct me if i am wrong.

Nov 19 '05 #4
Thanks to all for your answers !
FINALLY I could understand the use of FINALLY ;-)

Henri

"Henri" <hm********@hotmail.com> a écrit dans le message de
news:uM*************@TK2MSFTNGP12.phx.gbl...
This question is applicable to all the languages, but could someone provide me with an code example where FINALLY is really necessary.
FINALLY is often used for this kind of code:

Try
....
open a file
read some date
....
Catch
... dealing with the file error ....
Finally
close the file
End Try

But wouldn't the following (without FINALLY) work as well?

Try
....
open a file
read some date
....
Catch
... dealing with the file error ....
End Try
close the file

Please tell me what I'm missing.
Thanks
Henri


Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Brian Kelley | last post by:
def res(): try: a = 1 return finally: print "do I get here?" res() outputs "do I get here?"
9
by: David Stockwell | last post by:
In referring to my copy of the python bible, it tells me I can't use all three items 'try' except and finally. I can use the t/f or t/e combinations though What combination can i use if i want...
26
by: djw | last post by:
Hi, Folks- I have a question regarding the "proper" use of try: finally:... Consider some code like this: d = Device.open() try: d.someMethodThatCanRaiseError(...) if SomeCondition: raise...
4
by: Cameron Frasnelly | last post by:
The second to last lin of code in the below sub is causing VS2003 to tell me that "Name 'SqlDR' is not declared" when obviously it is. It's also used just fine in the main code... any ideas???? ...
2
by: Shi Mu | last post by:
very hard for me to understand the difference between try...except and try...finally
3
by: Arnaud Diederen | last post by:
Hello, I've been looking in the archives but found no interesting answer to my problem. I was wondering why the following code does not work in IE 5; I'd expect an alert showing 'In finally!',...
12
by: Carl J. Van Arsdall | last post by:
Hey python people, I'm interested in using the try/finally clause to ensure graceful cleanup regardless of how a block of code exits. However, I still am interested in capturing the exception....
12
by: David Lozzi | last post by:
Howdy, I ran into a very interesting issue and I'm curios as to how this is suppose to work. I am using Try...Catch...Finally statements for all database connectivity in my ASP.NET 2.0 web...
12
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.