By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,534 Members | 2,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,534 IT Pros & Developers. It's quick & easy.

try catch finally

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Tilfried Weissenberger wrote:
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

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog : http://weblogs.asp.net/FBouma
Microsoft MVP (C#)
Nov 16 '05 #2

P: n/a

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/de...pspec_8_10.asp

(beware of line wraps in the URL)


"Tilfried Weissenberger" <ti***@weissenberger.at> wrote in message
news:a9*************************@posting.google.co m...
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

Nov 16 '05 #3

P: n/a
On 30 Nov 2004 01:29:51 -0800, Tilfried Weissenberger wrote:
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.
--
Tom Porterfield
Nov 16 '05 #4

P: n/a
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

On 30 Nov 2004 01:29:51 -0800, Tilfried Weissenberger wrote:
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.
--
Tom Porterfield
=
Nov 16 '05 #5

P: n/a
Richard Blewett [DevelopMentor] <ri******@NOSPAMdevelop.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Richard Blewett [DevelopMentor] <ri******@NOSPAMdevelop.com> wrote:
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...

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
--
http://www.csharp-station.com
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.