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

发布于 2024-07-04 18:06:42 字数 393 浏览 6 评论 0原文

这些天,当我有一个实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野の 2024-07-11 18:06:42

是的,在 using 块中抛出异常没有什么不同。
请记住,using 块基本上翻译为:

IDisposable disposable = null;
try
{
    disposable = new WhateverYouWantedToMake();
}
finally
{
    disposable.Dispose()
}

因此,如果您想捕获任何东西,则必须滚动自己的捕获,但捕获/抛出与 using 完全不同。 最后几乎可以保证执行(除非出现无法捕获的异常(例如堆栈溢出或内存不足)或有人断电)。

Yeah there is nothing different about throwing exceptions in using blocks.
Remember that the using block basically translates to:

IDisposable disposable = null;
try
{
    disposable = new WhateverYouWantedToMake();
}
finally
{
    disposable.Dispose()
}

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).

网白 2024-07-11 18:06:42

您需要有一个 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

楠木可依 2024-07-11 18:06:42

我不太明白这个问题 - 你像平常一样抛出异常。
如果 MyThing 实现 IDisposable,则:

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

当您离开块时,将调用 thing.Dispose,因为抛出异常。 如果你想组合 try/catch/finally 和 using,你可以嵌套它们:(

try
{
    ...
    using ( MyThing thing = new MyThing() )
    {
        ...
    }
    ...
}
catch ( Exception e )
{
    ....
}
finally
{
    ....
}    

或者将 try/catch/finally 放在 using 中):

using ( MyThing thing = new MyThing() )
{
    ...
    try
    {
        ...
    }
    catch ( Exception e )
    {
        ....
    }
    finally
    {
        ....
    }    
    ...
} // thing.Dispose is called now

或者你可以展开 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文