try catch finally to close and dispose, but still wantApplication_error to fire

J

jobs

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 trigger my application_error event?

2. And/Or, Is there any way to close and dispose of connections left
behind by methods that might have failed without closing connections
in the application_error event?
 
G

Guest

Sorry,

I'm not sure if I have missed something from the picture, but I would
say... try --> catch --> throw

Inside catch if you throw the exception it will be bubbled up again.

HTH
Braulio
--
/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------
 
B

bruce barker

use try/finally without a catch, or c# using statement.

-- bruce (sqlwork.com)
 
A

Anthony Jones

jobs said:
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 trigger my application_error event?

2. And/Or, Is there any way to close and dispose of connections left
behind by methods that might have failed without closing connections
in the application_error event?

Use finally to close/dispose of connections. The Finally block always
executes.
If your purpose in creating a catch block is to simply close/dispose of
stuff before re-throwing the error then simply don't bother with the catch
block at all. The whole point of the finally block is to perform tidy up
code that must occur whether or not an exception is being thrown.
 
J

jobs

Use finally to close/dispose of connections. The Finally block always
executes.
If your purpose in creating a catch block is to simply close/dispose of
stuff before re-throwing the error then simply don't bother with the catch
block at all. The whole point of the finally block is to perform tidy up
code that must occur whether or not an exception is being thrown.

Thanks, But I do still need to make sure I close/dispose when an error
occurs. Can I do that in Appliation_error sub of the Global.asax? If
so, how? If not I will have to rethrow the same exception? how?
 
A

Anthony Jones

jobs said:
Thanks, But I do still need to make sure I close/dispose when an error
occurs. Can I do that in Appliation_error sub of the Global.asax? If
so, how? If not I will have to rethrow the same exception? how?

Let me emphasis what I've already said:-

"The Finally block always executes."

That means even if the flow of code is now in an error condition the code in
the finally block will still execute. Thats why its there. If code in the
finally block didn't execute when an error occured it wouldn't have any
point in being there since it would be no different than putting code at the
bottom of the try block.

So its in the finally block that you would place close/dispose code.

C# provides the using() alternative which allows you to define a block of
code that uses a disposable object that must always be disposed when
execution leaves block. The manner of leaving the block is immaterial,
either by normal code execution or due to an exception.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top