473,491 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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?
Nov 16 '07 #1
5 2527
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" wrote:
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?
Nov 16 '07 #2
use try/finally without a catch, or c# using statement.

-- bruce (sqlwork.com)
jobs wrote:
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?
Nov 16 '07 #3
"jobs" <jo**@webdos.comwrote in message
news:0b**********************************@f13g2000 hsa.googlegroups.com...
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.

--
Anthony Jones - MVP ASP/ASP.NET
Nov 16 '07 #4
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.
--
Anthony Jones - MVP ASP/ASP.NET
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?

Nov 16 '07 #5
"jobs" <jo**@webdos.comwrote in message
news:bf**********************************@a28g2000 hsc.googlegroups.com...
>
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.
--
Anthony Jones - MVP ASP/ASP.NET

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.
--
Anthony Jones - MVP ASP/ASP.NET


Nov 17 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3120
by: Big D | last post by:
Hi all, I'm trying to understand the best way to use a try catch with a SQL transaction. I have a number of sql statements that need to be run, such as: 'open sqlConnection, get command...
23
3038
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...
4
3636
by: Cameron Frasnelly | last post by:
The second to last lin of code in the below sub is causing VS2003 to tell me that "Name 'SqlDR' is not declared" when obviously it is. It's also used just fine in the main code... any ideas???? ...
13
1556
by: Woody Splawn | last post by:
I have a try catch statement in a fucntion that is supposed to return a true or a false My code looks like this: Try mySqlConnection.Open() Dim Da1 As New SqlDataAdapter("Select JnlType,...
26
2524
by: Grim Reaper | last post by:
Just a quick and probably daft question... Isn't the code; Try DoSomething() Catch e As Exception HandleError(e) Finally DoThisAtTheEnd()
32
6082
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
28
3800
by: RickHodder | last post by:
I'm getting frustrated with using try...catch with local variables: The code below wont compile in .NET 1.1: I get the following error: "Use of unassigned local variable 'oProcessFileReader' " ...
5
2307
by: Morten Snedker | last post by:
The use of Try in the Finally part - is that overkill?. I think of the code failing before opening sqlCon - that would generate an error in the Finally part. How would Finally handle that? Try...
12
2643
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...
0
7115
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
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.