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

  • Thread starter Thread starter jobs
  • Start date Start date
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?
 
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
/// ------------------------------
 
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.
 
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?
 
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.
 
Back
Top