是否可以执行以下操作:
我想捕获一个自定义异常并使用它做一些事情 - 很简单: try {...} catch (CustomException) {...}
但后来我想运行"catch all"块中使用的代码仍然运行一些与所有catch块相关的其他代码...
try
{
throw new CustomException("An exception.");
}
catch (CustomException ex)
{
// this runs for my custom exception
throw;
}
catch
{
// This runs for all exceptions - including those caught by the CustomException catch
}
Run Code Online (Sandbox Code Playgroud)
或者我必须在所有异常情况下放置我想要做的任何事情(finally不是一个选项,因为我只想运行异常)到一个单独的方法/嵌套整个try/catch在另一个(euch)... ?
我通常会做一些事情
try
{
throw new CustomException("An exception.");
}
catch (Exception ex)
{
if (ex is CustomException)
{
// Do whatever
}
// Do whatever else
}
Run Code Online (Sandbox Code Playgroud)