使用“ catch,When”捕获异常。 [英] Catching exceptions with "catch, when"

查看:190
本文介绍了使用“ catch,When”捕获异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了C#中的这一新功能,该功能允许在满足特定条件时执行捕获处理程序。

I came across this new feature in C# which allows a catch handler to execute when a specific condition is met.

int i = 0;
try
{
    throw new ArgumentNullException(nameof(i));
}
catch (ArgumentNullException e)
when (i == 1)
{
    Console.WriteLine("Caught Argument Null Exception");
}

我试图了解何时可能有用。

I am trying to understand when this may ever be useful.

一种情况可能是这样的:

One scenario could be something like this:

try
{
    DatabaseUpdate()
}
catch (SQLException e)
when (driver == "MySQL")
{
    //MySQL specific error handling and wrapping up the exception
}
catch (SQLException e)
when (driver == "Oracle")
{
    //Oracle specific error handling and wrapping up of exception
}
..

但这还是我可以做的相同的处理程序,并根据驱动程序的类型委托给不同的方法。这会使代码更容易理解吗?可以说不是。

but this is again something that I can do within the same handler and delegate to different methods depending on the type of the driver. Does this make the code easier to understand? Arguably no.

我可以想到的另一种情况是:

Another scenario that I can think of is something like:

try
{
    SomeOperation();
}
catch(SomeException e)
when (Condition == true)
{
    //some specific error handling that this layer can handle
}
catch (Exception e) //catchall
{
    throw;
}

同样,我可以这样做:

try
{
    SomeOperation();
}
catch(SomeException e)
{
    if (condition == true)
    {
        //some specific error handling that this layer can handle
    }
    else
        throw;
}

使用'catch,when'功能可以使异常处理更快,因为处理程序是这样跳过的,与在处理程序中处理特定用例相比,堆栈展开的时间要早​​得多?是否有任何更适合此功能的特定用例,然后人们可以作为一种好的做法?

Does using the 'catch, when' feature make exception handling faster because the handler is skipped as such and the stack unwinding can happen much earlier as when compared to handling the specific use cases within the handler? Are there any specific use cases that fit this feature better which people can then adopt as a good practice?

推荐答案

捕获块已经允许了您可以根据例外的 type 进行过滤:

Catch blocks already allow you to filter on the type of the exception:

catch (SomeSpecificExceptionType e) {...}

when 子句允许您扩展

因此,的情况下,使用 when 子句>异常的类型不足以确定是否应在此处处理该异常。

Thus, you use the when clause for cases where the type of the exception is not distinct enough to determine whether the exception should be handled here or not.

一个常见的用例是异常类型,它们实际上是包装器,用于多种不同类型的错误。

A common use case are exception types which are actually a wrapper for multiple, different kinds of errors.

我实际使用过的(在VB中已经有一段时间了):

Here's a case that I've actually used (in VB, which already has this feature for quite some time):

try
{
    SomeLegacyComOperation();
}
catch (COMException e) when (e.ErrorCode == 0x1234)
{
    // Handle the *specific* error I was expecting. 
}

SqlException 相同,还具有 ErrorCode 属性。另一种选择是这样的:

Same for SqlException, which also has an ErrorCode property. The alternative would be something like that:

try
{
    SomeLegacyComOperation();
}
catch (COMException e)
{
    if (e.ErrorCode == 0x1234)
    {
        // Handle error
    }
    else
    {
        throw;
    }
}

可以说它不那么优雅且轻微破坏堆栈跟踪

which is arguably less elegant and slightly breaks the stack trace.

此外,您可以提及相同的

In addition, you can mention the same type of exception twice in the same try-catch-block:

try
{
    SomeLegacyComOperation();
}
catch (COMException e) when (e.ErrorCode == 0x1234)
{
    ...
}
catch (COMException e) when (e.ErrorCode == 0x5678)
{
    ...
}

没有 when 条件是不可能的。

这篇关于使用“ catch,When”捕获异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆