Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > About FINALLY clause

Reply
Thread Tools

About FINALLY clause

 
 
Henri
Guest
Posts: n/a
 
      12-14-2004
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



 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      12-14-2004
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" <> wrote in message
news:...
> 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
>
>
>



 
Reply With Quote
 
 
 
 
Steve C. Orr [MVP, MCSD]
Guest
Posts: n/a
 
      12-14-2004
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" <> wrote in message
news:...
> 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
>
>
>



 
Reply With Quote
 
DKode
Guest
Posts: n/a
 
      12-14-2004
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.

 
Reply With Quote
 
Henri
Guest
Posts: n/a
 
      12-14-2004
Thanks to all for your answers !
FINALLY I could understand the use of FINALLY

Henri

"Henri" <> a écrit dans le message de
news:...
> 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
>
>
>
>




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Try...Catch...Finally not firing finally? David Lozzi ASP .Net 12 05-11-2007 12:41 AM
"else process" clause bxbxb3 VHDL 3 05-27-2005 11:41 AM
Re: Santa Clause like you have never seen him before Jeff Griffin Wireless Networking 0 12-29-2004 02:28 PM
Quartus II error - use clause error... - very strange behaviour Jan VHDL 2 12-16-2004 03:33 PM
Re: Formatting string in WHERE clause Lee Simpson ASP .Net 0 07-18-2003 04:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57