ram*_*ram 23 c# error-handling exception-handling exception
这里发布了一个非常相似的问题.我正在扩大这个问题.假设你想要捕获多种类型的异常,但想以同样的方式处理它,有没有办法做一些像switch case这样的事情?
switch (case)
{
case 1:
case 2:
DoSomething();
break;
case 3:
DoSomethingElse()
break;
}
Run Code Online (Sandbox Code Playgroud)
是否可以以相同的方式处理少数例外.就像是
try
{
}
catch (CustomException ce)
catch (AnotherCustomException ce)
{
//basically do the same thing for these 2 kinds of exception
LogException();
}
catch (SomeOtherException ex)
{
//Do Something else
}
Run Code Online (Sandbox Code Playgroud)
这是从另一个帖子复制的,但我将代码拉到这个线程:
捕捉System.Exception并开启类型
catch (Exception ex)
{
if (ex is FormatException || ex is OverflowException)
{
WebId = Guid.Empty;
return;
}
throw;
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢在几个 catch 块中重复一个方法调用。