我可以捕获特定或自定义异常吗?

sil*_*lla 6 .net c# exception-handling

我不想抓住一些例外.我能以某种方式做到吗?

我可以这样说:

catch (Exception e BUT not CustomExceptionA)
{
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*e B 21

try
{
      // Explosive code
}
catch (CustomExceptionA){ throw; }
catch (Exception ex)
{
    //classic error handling
}
Run Code Online (Sandbox Code Playgroud)

  • 我必须说,您的解决方案比我的要好! (2认同)

aba*_*hev 9

try
{
}
catch (Exception ex)
{
    if (ex is CustomExceptionA)
    {
        throw;
    }
    else
    {
        // handle
    }
}
Run Code Online (Sandbox Code Playgroud)


Jor*_*ker 5

从C#6开始,您可以使用异常过滤器

try
{
    // Do work
}
catch (Exception e) when (!(e is CustomExceptionA))
{
    // Catch anything but CustomExceptionA
}
Run Code Online (Sandbox Code Playgroud)