views:

1153

answers:

6

I'm trying to leverage the using block more and more these days when I have an object that implements IDisposable but one thing I have not figured out is how to catch an exception as I would in a normal try/catch/finally ... any code samples to point me in the right direction?

Edit: The question was modified after reading through the replies. It was "How to Throw an exception in a using block with .NET 2.0?" but I was actually looking for a way to catch these exceptions inside a using block.

+2  A: 

I'm confused. There's nothing special about throwing an exception inside a using block. What problem are you running into?

Brad Wilson
A: 

Agree with Brad can you give us some more info about the problems you are having? I have never had problems!

Rob Cooper
+3  A: 

Yeah there is nothing different about throwing exceptions in using blocks. Remember that the using block basically translates to:

IDisposable disposable = null;
try
{
    disposable = new WhateverYouWantedToMake();
}
finally
{
    disposable.Dispose()
}

So you will have to roll your own catching if you want to catch anything but catching/throwing is a completely separate concern from the using. The finally is almost guaranteed to execute (save an uncatchable exception (e.g. stackoverflow or outofmemory) or someone pulling the power out of the PC).

Quibblesome
+5  A: 

I don't really understand the question - you throw an exception as you normally would. If MyThing implements IDisposable, then:

using ( MyThing thing = new MyThing() )
{
    ...
    throw new ApplicationException("oops");
}

And thing.Dispose will be called as you leave the block, as the exception's thrown. If you want to combine a try/catch/finally and a using, you can either nest them:

try
{
    ...
    using ( MyThing thing = new MyThing() )
    {
        ...
    }
    ...
}
catch ( Exception e )
{
    ....
}
finally
{
    ....
}

(Or put the try/catch/finally in the using):

using ( MyThing thing = new MyThing() )
{
    ...
    try
    {
        ...
    }
    catch ( Exception e )
    {
        ....
    }
    finally
    {
        ....
    }    
    ...
} // thing.Dispose is called now

Or you can unroll the using and explicitly call Dispose in the finally block as @Quarrelsome demonstrated, adding any extra exception-handling or -recovery code that you need in the finally (or in the catch).

EDIT: In response to @Toran Billups, if you need to process exceptions aside from ensuring that your Dispose method is called, you'll either have to use a using and try/catch/finally or unroll the using - I don't thinks there's any other way to accomplish what you want.

Blair Conrad
A: 

I'm looking for more detail on rolling my own catching block inside a using block.

Edit: What I wanted to avoid is having to use a try/catch/finally inside my using block like @Blair showed. But maybe this is a non issue...

Edit: @Blair, this is exactly what I was looking for, thanks for the detailed reply!

Toran Billups
A: 

You need to have a try statement to catch an exception

Either you can use an try statement within the using block or you can use a using block in a try block

But you need to use a try block to catch any exceptions occuring

Fred