<p>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?</p>

<p>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.</p>

<hr>

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

<p>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...</p>

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

[{'answer_id': 36094, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 2, 'selected': False, 'text': '<p>Yeah there is nothing different about throwing exceptions in using blocks.\nRemember that the using block basically translates to:</p>\n\n<pre><code>IDisposable disposable = null;\ntry\n{\n    disposable = new WhateverYouWantedToMake();\n}\nfinally\n{\n    disposable.Dispose()\n}\n</code></pre>\n\n<p>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).</p>\n'}, {'answer_id': 36097, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 4, 'selected': True, 'text': '<p>I don\'t really understand the question - you throw an exception as you normally would.\nIf <code>MyThing</code> implements <code>IDisposable</code>, then:</p>\n\n<pre><code>using ( MyThing thing = new MyThing() )\n{\n    ...\n    throw new ApplicationException("oops");\n}\n</code></pre>\n\n<p>And <code>thing.Dispose</code> 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:</p>\n\n<pre><code>try\n{\n    ...\n    using ( MyThing thing = new MyThing() )\n    {\n        ...\n    }\n    ...\n}\ncatch ( Exception e )\n{\n    ....\n}\nfinally\n{\n    ....\n}    \n</code></pre>\n\n<p>(Or put the try/catch/finally in the using):</p>\n\n<pre><code>using ( MyThing thing = new MyThing() )\n{\n    ...\n    try\n    {\n        ...\n    }\n    catch ( Exception e )\n    {\n        ....\n    }\n    finally\n    {\n        ....\n    }    \n    ...\n} // thing.Dispose is called now\n</code></pre>\n\n<p>Or you can unroll the <code>using</code> and explicitly call <code>Dispose</code> in the <code>finally</code> block as @Quarrelsome demonstrated, adding any extra exception-handling or -recovery code that you need in the <code>finally</code> (or in the <code>catch</code>).</p>\n\n<p>EDIT: In response to @Toran Billups, if you need to process exceptions aside from ensuring that your <code>Dispose</code> method is called, you\'ll either have to use a <code>using</code> and <code>try/catch/finally</code> or unroll the <code>using</code> - I don\'t thinks there\'s any other way to accomplish what you want.</p>\n'}, {'answer_id': 2885551, 'author': 'Fred', 'author_id': 347472, 'author_profile': 'https://Stackoverflow.com/users/347472', 'pm_score': 0, 'selected': False, 'text': '<p>You need to have a try statement to catch an exception </p>\n\n<p>Either you can use an try statement within the using block or you can use a using block in a try block </p>\n\n<p>But you need to use a try block to catch any exceptions occuring </p>\n'}]
['https://Stackoverflow.com/questions/36081', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2701/']