473,387 Members | 1,698 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,387 software developers and data experts.

And Finally...


I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same as the next
lines of code? What's the difference between these two bits of code:

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}

and

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}

// Cleanup code
CloseFile();

Cheers

andyt

Nov 16 '05 #1
9 1474
Andy,

Easy. In the second section of code, if the WriteFile method throws an
exception, then the CloseFile method never gets called, as the method is
exited. The finally clause will guarantee that the code gets executed,
which seems important in this case, since you need to clean up resources
initialized in the method.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...

I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same as the next
lines of code? What's the difference between these two bits of code:

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}

and

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}

// Cleanup code
CloseFile();

Cheers

andyt

Nov 16 '05 #2
If Trace.Write happens to throw another exception - then CloseFile wil never
gets executed in the second scenario.

"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...

I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same as the next
lines of code? What's the difference between these two bits of code:

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}

and

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}

// Cleanup code
CloseFile();

Cheers

andyt

Nov 16 '05 #3
so, how do you fix that?

--
Saludos,

Jose Luis Manners, MCP

"Marina" <so*****@nospam.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
If Trace.Write happens to throw another exception - then CloseFile wil never gets executed in the second scenario.

"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...

I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same as the next
lines of code? What's the difference between these two bits of code:

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}

and

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}

// Cleanup code
CloseFile();

Cheers

andyt


Nov 16 '05 #4
Hi, Andy

main issue with second variant is very clear if you put return; into catch
clause. For such case only finally block guarantees that file will be closed
before control will be passed out of current routine.

HTH
Alex

"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...

I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same as the next
lines of code? What's the difference between these two bits of code:

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}

and

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}

// Cleanup code
CloseFile();

Cheers

andyt

Nov 16 '05 #5
You fix it by using the first code sample, which does use the Finally block.

"Jose Luis Manners" <jlmanners(-arroba-)acm.org> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
so, how do you fix that?

--
Saludos,

Jose Luis Manners, MCP

"Marina" <so*****@nospam.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
If Trace.Write happens to throw another exception - then CloseFile wil

never
gets executed in the second scenario.

"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...

I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same as the next
lines of code? What's the difference between these two bits of code:

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}

and

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}

// Cleanup code
CloseFile();

Cheers

andyt



Nov 16 '05 #6
Ah, OK that's what I figured. Thanks.

--
Regards,

Jose Luis Manners, MCP

"Marina" <so*****@nospam.com> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl...
You fix it by using the first code sample, which does use the Finally block.
"Jose Luis Manners" <jlmanners(-arroba-)acm.org> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
so, how do you fix that?

--
Saludos,

Jose Luis Manners, MCP

"Marina" <so*****@nospam.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
If Trace.Write happens to throw another exception - then CloseFile wil

never
gets executed in the second scenario.

"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...
>
> I was just pondering about what the Finally in an exception actually
> does. Well, I realise that it always gets called regardless of whether > the exception is thrown or not, but isn't that the same as the next
> lines of code? What's the difference between these two bits of code:
>
> try
> {
> WriteFile();
> }
> catch
> {
> // Oh dear
> Trace.Write ("Data lost");
> }
> finally
> {
> // cleanup code
> CloseFile();
> }
>
>
>
> and
>
>
>
> try
> {
> WriteFile();
> }
> catch
> {
> // Oh dear
> Trace.Write ("Data lost");
> }
>
> // Cleanup code
> CloseFile();
>
>
>
> Cheers
>
>
>
> andyt
>



Nov 16 '05 #7
There's not a tremendous amount of difference in your example (except for
differences already mentioned, like if something in the catch block throws).

Use of the finally becomes a lot more obvious if you have a method that is
only looking for a specific type of exception, like this:

try
{
WriteFile();
}
catch (SomeApplicationExceptionTypeThatICanHandle e)
{
// Oh dear
Trace.Write("Data Lost. Message: " + e.message);
}
finally
{
CloseFile();
}
Note that if some exception other than the one that I am looking for occurs,
the catch never gets executed, but the close file will still be called. Or,
maybe you have a method that doesn't care so much about the exception (the
error handling code occurs "higher up" in the stack:

try
{
WriteFile();
}
finally
{
CloseFile();
}
The finally lets you make a distinction between what is supposed to happen
when the method exits, regardless of the exception, and the actual error
handling code. The error handling (catch) code can even be relocated to a
different method in the call chain.

"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...

I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same as the next
lines of code? What's the difference between these two bits of code:

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}

and

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}

// Cleanup code
CloseFile();

Cheers

andyt

Nov 16 '05 #8
Yep. And things get even clearer if the method doesn't handle the exception
itself, i.e. doesn't catch it or rethrows it.
This:

try
{
WriteFile();
}
finally
{
// cleanup code
CloseFile();
}

is much cleaner than:

try
{
WriteFile();
}
catch
{
// Oh dear
CloseFile();
throw;
}
// cleanup code
CloseFile();

Especially if more than one line of cleanup is needed.

Note that the using clause has a similar effect; It calls Dispose in a
finally block.

Niki

"J.Marsch" <je****@ctcdeveloper.com> wrote in
news:ut**************@TK2MSFTNGP10.phx.gbl...
There's not a tremendous amount of difference in your example (except for
differences already mentioned, like if something in the catch block throws).
Use of the finally becomes a lot more obvious if you have a method that is
only looking for a specific type of exception, like this:

try
{
WriteFile();
}
catch (SomeApplicationExceptionTypeThatICanHandle e)
{
// Oh dear
Trace.Write("Data Lost. Message: " + e.message);
}
finally
{
CloseFile();
}
Note that if some exception other than the one that I am looking for occurs, the catch never gets executed, but the close file will still be called. Or, maybe you have a method that doesn't care so much about the exception (the
error handling code occurs "higher up" in the stack:

try
{
WriteFile();
}
finally
{
CloseFile();
}
The finally lets you make a distinction between what is supposed to happen
when the method exits, regardless of the exception, and the actual error
handling code. The error handling (catch) code can even be relocated to a
different method in the call chain.

"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...

I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same as the next
lines of code? What's the difference between these two bits of code:

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}

and

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}

// Cleanup code
CloseFile();

Cheers

andyt


Nov 16 '05 #9
if your thread is aborting, paritcularly important is asp.net - you'll for
sure want the finally clause.

"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...

I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same as the next
lines of code? What's the difference between these two bits of code:

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}

and

try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}

// Cleanup code
CloseFile();

Cheers

andyt

Nov 16 '05 #10

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

Similar topics

4
by: Brian Alexander | last post by:
Hello; I'm curious to know how people preserve exceptions that arise in a try ... finally block. Consider this example: try: getResource() doSomething() finally: alwaysFreeResource()
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...
16
by: Ken | last post by:
What is the purpose of a finally block in try-catch- finally? I am confused because, if I am reading the definition correctly, this block seems unnecessary. Consider the following from the Visual...
16
by: Bill | last post by:
Say I have a childThread currently is running a finally block to cleanup external resources. At the same time the main thread calls childThread.Abort(). The question is: when the...
3
by: Brian | last post by:
Is it possible to have more than one try/catch/finally block within a method? Also, if the code never enters the try block of code, is the finally block executed? I have a try block in a branch...
8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
16
by: Chris | last post by:
Hi, regarding exception-handling : why put code in a 'finally' block, and not just after a 'catch'-block --> Example using 'finally ' : try { OpenFile();
7
by: Sean Kirkpatrick | last post by:
I got caught with my pants down the other day when trying to explain Try...Catch...Finally and things didn't work as I had assumed. Perhaps someone can explain to me the purpose of Finally. I've...
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!',...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.