Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

But with a 'using' you can't catch an exception. So in production I end up using ( ;-) ) try catch finally.


The using block compiles to the equivalent of a try-finally block. If you want to catch an exception you can do it inside or outside the using block.


There are known issues with using "using". If the Dispose() call throws an exception in the finally block, then any exception that occurred in the within the using block is masked.

The easy solution is to make sure that your Dispose() calls don't throw exceptions, unfortunately some of Microsoft's classes don't conform to this (e.g. WCF clients).

The following MSDN article shows a case where they do not recommend using "using", and instead suggest explicitly using try-catch-finally: http://msdn.microsoft.com/en-us/library/aa355056.aspx


> with a 'using' you can't catch an exception

[Citation needed]


OK let me rephrase it : the 'using' isn't catching the exceptions that are thrown inside its scope so you still have to use a 'catch' block.

Then what's the point of having a 'using + catch' instead of a 'try catch finally' ?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: