如何使用 .NET 2.0 捕获 using 块中的异常?
这些天,当我有一个实现 IDisposable 的对象时,我越来越多地尝试利用 using 块,但我还没有弄清楚的一件事是如何像在正常的 try/catch/finally 中一样捕获异常......有任何代码示例可以为我指明正确的方向吗?
编辑:阅读回复后修改了问题。 这是“如何在 .NET 2.0 的 using 块中抛出异常?” 但我实际上正在寻找一种方法来捕获 using 块中的这些异常。
我正在寻找有关在 using 块内滚动我自己的捕获块的更多详细信息。
编辑:我想避免的是必须在我的 using 块中使用 try/catch/finally,如@Blair 所示。 但也许这不是问题......
编辑:@Blair,这正是我正在寻找的,感谢您的详细回复!
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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,在 using 块中抛出异常没有什么不同。
请记住,using 块基本上翻译为:
因此,如果您想捕获任何东西,则必须滚动自己的捕获,但捕获/抛出与 using 完全不同。 最后几乎可以保证执行(除非出现无法捕获的异常(例如堆栈溢出或内存不足)或有人断电)。
Yeah there is nothing different about throwing exceptions in using blocks.
Remember that the using block basically translates to:
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).
您需要有一个 try 语句来捕获异常
您可以在 using 块中使用 try 语句,也可以在 try 块中使用 using 块
但是您需要使用 try 块来捕获发生的任何异常
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
我不太明白这个问题 - 你像平常一样抛出异常。
如果
MyThing
实现IDisposable
,则:当您离开块时,将调用
thing.Dispose
,因为抛出异常。 如果你想组合 try/catch/finally 和 using,你可以嵌套它们:(或者将 try/catch/finally 放在 using 中):
或者你可以展开
using
并明确如 @Quarrelsome 所示,在finally
块中调用Dispose
,在finally
中添加所需的任何额外异常处理或恢复代码(或在catch
中)。编辑:响应@Toran Billups,如果除了确保调用
Dispose
方法之外还需要处理异常,则必须使用using
和 < code>try/catch/finally 或展开using
- 我认为没有其他方法可以完成您想要的任务。I don't really understand the question - you throw an exception as you normally would.
If
MyThing
implementsIDisposable
, then: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:(Or put the try/catch/finally in the using):
Or you can unroll the
using
and explicitly callDispose
in thefinally
block as @Quarrelsome demonstrated, adding any extra exception-handling or -recovery code that you need in thefinally
(or in thecatch
).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 ausing
andtry/catch/finally
or unroll theusing
- I don't thinks there's any other way to accomplish what you want.