473,324 Members | 2,417 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,324 software developers and data experts.

finally block

Hi,

Once the execution enters the "finally" block, the execution could have
followed

1. normal/non-exception execution. (I guess everyone knows what I mean here.)
2. Exception was thrown, and handled.
3. Unhandled exception.

Is there a way to find out which of above three has happened while in
"finally" block? In specific, I would like to know whether the execution is
in "unhandled" exception state or "others (don't care to distinguish between
1 and 2)" state.

Thanks,

--
George
Aug 21 '06 #1
7 1579
George,
>Is there a way to find out which of above three has happened while in
"finally" block?
Nothing built into the language, but you could keep track of that
yourself by setting some flag. But if you need this, it sounds like
you've got a design problem.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 21 '06 #2

George wrote:
Hi,

Once the execution enters the "finally" block, the execution could have
followed

1. normal/non-exception execution. (I guess everyone knows what I mean here.)
2. Exception was thrown, and handled.
3. Unhandled exception.

Is there a way to find out which of above three has happened while in
"finally" block? In specific, I would like to know whether the execution is
in "unhandled" exception state or "others (don't care to distinguish between
1 and 2)" state.

Thanks,

--
George
Easiest way is to declare a couple of bools.

bool exThrown, exHandled

try
//some exception-throwing code
catch (Exception ex)
{
exThrown = true;
//try to handle
//if you handle it successfully...
exHandled = true;
//otherwise...
exHandled = false;
throw ex;
}
finally
{
//decide what to do based on the values of exThrown and exHandled
}

You should know which specific exceptions you can successfully handle
and catch those in specific clauses. If you ever have to rethrow, make
sure exHandled is false. Besides the specific exceptions, catch any
other Exceptions that are thrown in a catch-all, set exThrown to true,
then immediately rethrow. Finally will then know two things; whether an
exception was thrown, and whether it was completely handled.

You could also put the case-specific code where it would only be
executed under the conditions you want. Put the code that should
execute when no exception is thrown at the very end of the try block,
put code that should happen when you handle an exception at the end of
the catch handler, put code for unhandled exceptions in a catch-all and
then rethrow, and put code that should execute regardless of the
situation in the finally block.

Aug 21 '06 #3
As was mentioned before, you shouldn't need to write code to find out
what's about to happen when you leave finally. Finally is for code that
should happen no matter what, such as destroying objects and
Datareaders that, no matter what you're doing next, you don't need and
want garbage collection to clear from memory.

Aug 21 '06 #4
Hi,

Thanks for the input.

I don't really need this capability in the language, however, I do think
that it will simplify my code. I do not think the need to use it is a design
issue either.

If you take a look of the following 2 methods:

1. MyFunction_A() is the original code and MyFunction_B() "untilizes" the
ability to know whether the execution is in "unhandled" state. Of course,
this ability does not exist.

2. CleanUp() is called if the
a. animal is a monkey
b. when exception is not handled, or will be rethrown.

public void MyFunction_A()
{
...

try
{
...
if (typeOfAnimal == Animal.Monkey)
{
CleanUp();
}

}
catch (JobUtils.AutomationException ex)
{
CleanUp();
throw (ex);
}
catch (Exception ex)
{
CleanUp();
throw (ex);
}
return;
}

public void MyFunction_B()
{
...

try
{
...
}
catch (JobUtils.AutomationException ex)
{
throw (ex);
}
catch (Exception ex)
{
throw (ex);
}
finally
{
if (typeOfAnimal == Animal.Monkey || <Unhandled exception state>)
{
CleanUp();
}
}

return;
}

--
George
"li****@gmail.com" wrote:
As was mentioned before, you shouldn't need to write code to find out
what's about to happen when you leave finally. Finally is for code that
should happen no matter what, such as destroying objects and
Datareaders that, no matter what you're doing next, you don't need and
want garbage collection to clear from memory.

Aug 22 '06 #5

"George" <wa**@nospam.nospamwrote in message
news:B5**********************************@microsof t.com...
Once the execution enters the "finally" block, the execution could have
followed

1. normal/non-exception execution. (I guess everyone knows what I mean
here.)
2. Exception was thrown, and handled.
3. Unhandled exception.

Is there a way to find out which of above three has happened while in
"finally" block? In specific, I would like to know whether the execution
is
in "unhandled" exception state or "others (don't care to distinguish
between
1 and 2)" state.
Don't really understand why you need to do it, but maybe something like
this:
System.Exception Exception = null;
try {
...
} catch (System.NullReferenceException Ex) {
Exception = Ex;
} catch (System.ArgumentException Ex) {
Exception = Ex;
} finally {
if (Exception != null) {
if (Exception is System.NullReferenceException) ...
else if (Exception is System.ArgumentException) ...
else ...
}
}

-- Alan
Aug 22 '06 #6
Hi George,

Thanks for your feedback!

Yes, I understand your concern. As I think, providing the information of
whether current code is coming from an exception or normal execution path
at language level is useful . I am not sure why this feature is not
provided in C# language. I will help you to consult it in our C# team.

Currently, I think the simplest solution to this problem is using bool
flags in all the possible execution paths. Below code snippet demonstrates
this:

private void method()
{
bool fNormal=false, fCatch=false, fUnhandled=false;
try
{
...
fNormal=true; //just before exiting the try clause
}
catch(Specific Exception)
{
fCatch=true;
...
}
finally
{
if(fNormal)
{
...
}
else if(fCatch)
{
}
else
{
fUnhandled=true;
}
}
}
Does this meet your need? If I misunderstand you, please feel free to tell
me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 23 '06 #7
Hi George,

Sorry for letting you wait.

After discussing internally, I was told that since few developers require
this feature, so the system is not burdened with setting up flags which
rarely are used. I think the bool flag workaround is the suitable for your
scenario. Does it meet your need? Please feel free to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 24 '06 #8

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()
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();
24
by: Dave | last post by:
Maybe I'm missing something here, but I can't see the purpose of the 'finally' keyword. What is the difference between: try { doSomething() } catch { handleError(); }
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...
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!',...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.