如何在.NET 2.0的使用块中捕获异常?

编程入门 行业动态 更新时间:2024-10-22 10:43:09
本文介绍了如何在.NET 2.0的使用块中捕获异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 当我有一个实现IDisposable的对象时,我试图越来越多地利用使用块,但是我还没有想到的是如何捕获一个异常,就像我在一个正常的try / catch / finally ...任何代码示例来指向我正确的方向?

编辑:问题在阅读回复后被修改。它是如何使用.NET 2.0在使用块中抛出异常?但我实际上正在寻找一种在使用块中捕获这些异常的方法。

我正在寻找更多的细节在一个使用块中滚动我自己的捕获块。

编辑:我想避免的是不得不使用try / catch / finally在我的使用块像@Blair显示。但是也许这是一个非问题...

编辑:@Blair,这正是我正在寻找的,谢谢详细的回复!

解决方案

我真的不明白这个问题 - 你通常会抛出异常。 如果 MyThing 实现 IDisposable ,那么:

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

和 thing.Dispose try { ... using(MyThing thing = new MyThing()) { ... } ... } catch(异常e) { .... } 终于 { ....

(或者将try / catch / finally放在使用中):

using(MyThing thing = new MyThing()) { ... try { ... } catch(异常e) { .... } finally { .... } ... } // thing.Dispose现在调用

或者您可以使用展开,并显式调用 Dispose code> finally block as @Quarrelsome演示,添加任何额外的异常处理或-recovery代码,您需要在 finally (或 catch )。

编辑:回应到@Toran Billups,如果您需要处理异常,除非确保调用 Dispose 方法,否则您必须使用使用和 try / catch / finally 或使用展开 - 我不认为有其他方法来完成你想要的。

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!

解决方案

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.

更多推荐

如何在.NET 2.0的使用块中捕获异常?

本文发布于:2023-11-15 14:00:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   如何在   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!