I do not quite understand the question - you are making an exception, as usual. If MyThing implements IDisposable , then:
using ( MyThing thing = new MyThing() ) { ... throw new ApplicationException("oops"); }
And thing.Dispose will be called when you leave the block as an exception. If you want to combine try / catch / finally and usage, you can either nest them:
try { ... using ( MyThing thing = new MyThing() ) { ... } ... } catch ( Exception e ) { .... } finally { .... }
(Or put try / catch / finally when using):
using ( MyThing thing = new MyThing() ) { ... try { ... } catch ( Exception e ) { .... } finally { .... } ... }
Or you can expand using and explicitly call Dispose in the finally block, as @Quarrelsome demonstrated, by adding the extra exception-handling or -recovery code that you need in finally (or catch ).
EDIT: in response to @Toran Billups, if you need to handle exceptions, except that you call your Dispose method, you will either have to use using , or try/catch/finally , or deploy using - I don't think there is another way accomplish what you want.
source share