try catch finally

  • Thread starter Tilfried Weissenberger
  • Start date
T

Tilfried Weissenberger

Hi,

I am a bit confused as to what the FINALLY block is meant for.

What's the difference between:

this.Cursor = Cursors.WaitCursor;
try { //do some stuff }
catch { //handle exception }
finally { this.Cursor = Cursors.Default; }


and this one:

this.Cursor = Cursors.WaitCursor;
try { //do some stuff }
catch { //handle exception }
this.Cursor = Cursors.Default;

regards, Tilli
 
F

Frans Bouma [C# MVP]

Tilfried said:
Hi,

I am a bit confused as to what the FINALLY block is meant for.

What's the difference between:

this.Cursor = Cursors.WaitCursor;
try { //do some stuff }
catch { //handle exception }
finally { this.Cursor = Cursors.Default; }
and this one:

this.Cursor = Cursors.WaitCursor;
try { //do some stuff }
catch { //handle exception }
this.Cursor = Cursors.Default;

In the last situation, if an exception is thrown that is not handled,
the line 'this.Cursor = Cursors.Default;' is never called, while in the
first situation, that line is always called, i.e.: you can create
flexible exception handling situations:

try
{
// some code
}
catch(...)
{
// catch only the exceptions you can handle HERE
// the rest will bubble up!
}
finally
{
// do tear down/clean up
}

FB
 
D

Dan Bass

snippet from the MSDN:

"The statements of a finally block are always executed when control leaves a
try statement. This is true whether the control transfer occurs as a result
of normal execution, as a result of executing a break, continue, goto, or
return statement, or as a result of propagating an exception out of the try
statement.

If an exception is thrown during execution of a finally block, the exception
is propagated to the next enclosing try statement. If another exception was
in the process of being propagated, that exception is lost. The process of
propagating an exception is discussed further in the description of the
throw statement."

http://msdn.microsoft.com/library/d...ry/en-us/csspec/html/vclrfcsharpspec_8_10.asp

(beware of line wraps in the URL)
 
T

Tom Porterfield

I am a bit confused as to what the FINALLY block is meant for.

What's the difference between:

this.Cursor = Cursors.WaitCursor;
try { //do some stuff }
catch { //handle exception }
finally { this.Cursor = Cursors.Default; }

and this one:

this.Cursor = Cursors.WaitCursor;
try { //do some stuff }
catch { //handle exception }
this.Cursor = Cursors.Default;

One of the most common scenarios where you would see the difference is if
an exception occurred in your catch statement. Say in your catch you
wanted to log the exception to the event log or to an application log file,
but a permission or some other error occurred and an exception was thrown
while trying to log the initial exception. In that case, with your second
example the resetting of the cursor would not be executed. In your first
example it would be executed.
 
R

Richard Blewett [DevelopMentor]

In fact, I'd argue that you should normally see the finally keyword in an application far more often than the catch keyword - or at least the finally keyword in disguise via using or lock which both emit a try ... finally block under the covers.

There is only any point in trying to catch an exception if you can do something about it - so maybe a user has slected a file to open and that file is locked by another process. Its worth catching the FileIOException and telling the user so they can close the other process and retry. But what should you do about a NullReferenceException?

Obviously you would want to handle this exception *somewhere* so the user doesn't just see ann unhandled exception but you probably in this case just want to take the application down gracefully logging the problem.

finally blocks on the other hand (either explicit or implicit ones generated by using and lock) are a fundemental requirement to ensure robust resource cleanup for methods, therefore, you should have many more of them than catch blocks.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I am a bit confused as to what the FINALLY block is meant for.

What's the difference between:

this.Cursor = Cursors.WaitCursor;
try { //do some stuff }
catch { //handle exception }
finally { this.Cursor = Cursors.Default; }

and this one:

this.Cursor = Cursors.WaitCursor;
try { //do some stuff }
catch { //handle exception }
this.Cursor = Cursors.Default;

One of the most common scenarios where you would see the difference is if
an exception occurred in your catch statement. Say in your catch you
wanted to log the exception to the event log or to an application log file,
but a permission or some other error occurred and an exception was thrown
while trying to log the initial exception. In that case, with your second
example the resetting of the cursor would not be executed. In your first
example it would be executed.
 
J

Jon Skeet [C# MVP]

Richard Blewett said:
In fact, I'd argue that you should normally see the finally keyword
in an application far more often than the catch keyword - or at least
the finally keyword in disguise via using or lock which both emit a
try ... finally block under the covers.

I certainly agree - but I can't remember the last time I actually *did*
use the finally keyword itself, interestingly enough. The using (and
lock) keywords seem to cover almost every case where I used to use
finally in Java...
 
J

Joe Mayo

Jon Skeet said:
I certainly agree - but I can't remember the last time I actually *did*
use the finally keyword itself, interestingly enough. The using (and
lock) keywords seem to cover almost every case where I used to use
finally in Java...


It depends on the situation for me. When I'm writing my own code, I'll tend
to use using, but when I'm showing someone else how to do it, I'll use
finally to make the point.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top