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

Need best practice on handling exceptions caused by the Dispose me

What are the best practice on handling an exception caused by a Dispose
method when its called from inside a loop? Wrap the entire loop in a
try-catch or do the try-catch on each iteration to get as many of the object
Disposed as possible?

Take the following example:

Bitmap[] bmpArray = ... // Some fancy bitmap array.
try
{
// Som bitmap operation like adding a watermark.
}
finally
{
// We need to Dispose of every bitmap in the array.
foreach(Bitmap bmp in bmpArray)
{
// Aside from the comments I think this is a pretty normal finally
construct.
if(bmp != null)
bmp.Dispose(); // Note: This line could throw a
SafeNativeMethods.StatusException!
// Now for arguments sake let's say that it's more important that as
many of the pictures are being disposed as possible than it is to make it go
quick by wrapping the entire loop in a try-catch... then what do you do?
// Do I have to wrap the Dispose call in it's own try-catch? What are
the perf implications?
}
}

Are there some common rules about Dispose/Close and exceptions getting
thrown by those two?

Thanks in advance...

--
Johannes Hansen

System Consultant, frontAvenue A/S
Aug 29 '05 #1
4 1795
Johannes,

Can you enlighten us about what dispose you are talking. Probably not the
bitmap dispose because that implements the Idisposable. AFAIK don't both
have a catchable exception.

Cor
Aug 29 '05 #2
Well, if you use reflector to reverse engineer the Image.Dispose method
you'll see that it is capable of throwing the
SafeNativeMethods.StatusException. So I guess my question is: What should I
do to handle exceptions in Dispose? Should I care about
SafeNativeMethods.StatusException and exceptions thrown from other Dispose
methods? Every try..finally I've seen doesn't seem to care if the Dispose
itself fails... not even the c# "using" statement seems to care if the
Dispose fails.

--
Johannes Hansen

System Consultant, frontAvenue A/S
"Cor Ligthert [MVP]" wrote:
Johannes,

Can you enlighten us about what dispose you are talking. Probably not the
bitmap dispose because that implements the Idisposable. AFAIK don't both
have a catchable exception.

Cor

Aug 30 '05 #3
You need to treat an exception thrown from a Dispose method the same as you
do from any other method...you must analyze it to determine what will happen
is an exception is thrown from various points within the method, if you want
to catch any of the exceptions, and what happens if an exception bubbles up
from the method. The Dispose method itself is the same as any other method,
with the one distinction being that the C# using statement automatically
invokes the Dispose method in the finally block section that it creates.
As to exceptions thrown in a loop, you probably need to catch each one
separately as you may leak resources if you don't free each bitmap.

The perf implications are that each exception thrown has all the overhead of
an exception (SEH stackwalk), so if each bitmap throws an exception it
could take a long time to execute. The question then becomes one of
determining why so many are throwing exceptions; a related question is if
you can detect in advance when it would throw an exception so you can avoid
calling it.

The perf downside of iterating a loop with a try-catch in it is minimal if
no exceptions are thrown.

"Johannes Hansen" <Jo************@discussions.microsoft.com> wrote in
message news:A6**********************************@microsof t.com...
Well, if you use reflector to reverse engineer the Image.Dispose method
you'll see that it is capable of throwing the
SafeNativeMethods.StatusException. So I guess my question is: What should
I
do to handle exceptions in Dispose? Should I care about
SafeNativeMethods.StatusException and exceptions thrown from other Dispose
methods? Every try..finally I've seen doesn't seem to care if the Dispose
itself fails... not even the c# "using" statement seems to care if the
Dispose fails.

--
Johannes Hansen>
System Consultant, frontAvenue A/S
"Cor Ligthert [MVP]" wrote:
Johannes,

Can you enlighten us about what dispose you are talking. Probably not the
bitmap dispose because that implements the Idisposable. AFAIK don't both
have a catchable exception.

Cor

Aug 30 '05 #4


Johannes Hansen wrote:
Well, if you use reflector to reverse engineer the Image.Dispose method
you'll see that it is capable of throwing the
SafeNativeMethods.StatusException. So I guess my question is: What should I
do to handle exceptions in Dispose? Should I care about
SafeNativeMethods.StatusException and exceptions thrown from other Dispose
methods? Every try..finally I've seen doesn't seem to care if the Dispose
itself fails... not even the c# "using" statement seems to care if the
Dispose fails.


Are you certain you don't just wish to abort program execution if that
exception occurs? This means you should do no special handling of it.

Before you drown yourself in considerations about this exception, you
should find out under what curcumstances it would be throw. It is quite
likely that this exception indicates some internal inconsistency, which
should not occur in your program.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Aug 30 '05 #5

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

Similar topics

9
by: Mark Twombley | last post by:
Hi, I'm just getting back into C++ and had a question about the best practice for assigning error numbers. I have been working in VB for sometime now and there you would start assigning error...
6
by: Jesper Ordrup Christensen | last post by:
Say I've created a piece of code that involves both sql, io and some number conversions. Being a responsible developer I have tried to catch all of the exceptions - but how can I be sure? Is...
0
by: Joe | last post by:
Reposting here as there were no useful replies in the dotnet.framework NG... What is the correct pattern for handling exceptions in IDisposable.Dispose, especially in a class that manages...
5
by: Lau Lei Cheong | last post by:
Hello, I'm currently using Application_Error method in Global.asax.cs to handle errors. Recently, I heard about Page.ErrorPage and plan to use it for handling errors on certain pages. Are...
4
by: Johannes Hansen | last post by:
What are the best practice on handling an exception caused by a Dispose method when its called from inside a loop? Wrap the entire loop in a try-catch or do the try-catch on each iteration to get...
1
by: GS | last post by:
Any points of what would be the good error handling design for application? User error handling in Application_OnError and throw() new errors on conditions through the code? I'd like utlimiately to...
6
by: RonL | last post by:
What is the recommended best technique for handling errors/exceptions in ASP.Net. I've read about the following techniques: 1. Try/Catch 2. Page_Error 3. Application_Error in the...
5
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored...
41
by: =?Utf-8?B?VGltIE1hcnNkZW4=?= | last post by:
Hi, I am after suggestions on the best practice declaring and destroying objects. some example code: Private Sub MySub Dim frmMyForm As MyForm Try
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: 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
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.