473,385 Members | 1,753 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.

try catch finally

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
6 7111
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

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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roger Redford | last post by:
Dear experts, I'm trying to learn java on my own. I picked up a sample online, but it is not compiling right: ------------------------------------------------ import java.io.*;
5
by: C# beginner | last post by:
Hi all If inside a try-catch-finally block, return statements are used, will the finally block get executed or not? Your replies are very important to me. Thanks a lot.
17
by: Alexander Mueller | last post by:
I have always wondered what finally is actually for. What would be the exact difference between try { somecode1(); } catch(Exception) { somecode2();
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
2
by: Ralph Krausse | last post by:
I created a try/catch/finally but when an expection is thrown, the catch does not handle it... (I know this code is wrong, I want to force the error for this example) try { DataSet ds = new...
7
by: John Dann | last post by:
I'm tripping over a silly little problem when trying to read a text file, but can't see the fix. Outline code is: ---------------------------------------------------- FS=New FileStream(filename,...
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...
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...
5
by: jobs | last post by:
re: try catch finally to close and dispose, but still want Application_error to fire 1. If catch an exception to to dispose and close of a ado connect, how can I allow the exception to still...
2
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi, I'm wondering if anyone can give me any ideas/good practice/advice. I've got a web form which a user inputs lots of data into, then presses submit. The submit button uses two classes to input...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.