Re: Is this pattern OK?

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Fri, 18 Jan 2008 11:50:43 -0800, richard.markiewicz@xxxxxxxxxxxx <richard.markiewicz@xxxxxxxxxxxx> wrote:

So what you are saying is, if my original snippet looked like this:

finally
{
objSearchResults.Dispose(); //The troublesome line is now first in the
finally block
objSearchADAM.Dispose();
objADAM.Dispose();
}

That the second and third dispose statements would not be executed
after the first one threw an exception?

Yes. That said, you should of course not have your own code have bugs that throw exceptions, and IMHO the Dispose() method should never throw an exception. But the "using" statement provides the "belts and suspenders" to ensure that even if those rules are violated, things get cleaned up.

Do you know what Using{} does behind the scenes? Does it create a
try{} finally{} on the objects I "use"?

Yes. The "using" statement is essentially equivalent to:

IDisposable obj = // whatever your variable is;

try
{
// code in the using block
}
finally
{
obj.Dispose();
}

Nested "using" statements nest the whole thing:

IDisposable obj1 = ...;

try
{
IDisposeable obj2 = ...;

try
{
// code in the using block
}
finally
{
obj2.Dispose();
}
}
finally
{
obj1.Dispose();
}

Thus even if an inner call to Dispose() throws an exception, the outer ones still get executed.

Note also that the "using" statement essentially generates a private variable that it uses for the call to Dispose(). It doesn't matter what you do to the actual declared variable in your own code, the "using" statement will always call Dispose() on the original reference.

Pete
.



Relevant Pages

  • RE: Wrapping Word.Application object with an IDisposable class?
    ... there is an unhandled exception in my program, ... Word process is not closed in Dispose method when the application is tested ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.vb)
  • Re: main form, IDisposable, and Application.Run
    ... exception is thrown between construction and the method call. ... open forms (which would obviously dispose the form). ... Maybe the exception is being thrown from the constructor after all, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: AccessViolationException and Native Exception
    ... dispose a data reader you've already closed or something like that? ... while I prepare the sample you asked me, I send you the exception ... the following exception is thrown: ... and each method opens the connection, ...
    (microsoft.public.sqlserver.ce)
  • Re: Is this pattern OK?
    ... //The troublesome line is now first in the ... IDisposable obj1 = ...; ... Thus even if an inner call to Dispose() throws an exception, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Retrieve thrown exception in using (disposable) cleanup
    ... It's generally suggested not to throw exceptions in Dispose: ... class MySession:: IDisposable ... > class MySession: IDisposable ... > already thrown exception as an inner exception in its own exception that ...
    (microsoft.public.dotnet.languages.csharp)