gpt4 book ai didi

c# - 对所有捕获重用捕获

转载 作者:太空狗 更新时间:2023-10-29 22:22:57 25 4
gpt4 key购买 nike

是否可以做类似下面的事情:

我想捕获自定义异常并对其进行处理 - 简单:try {...} catch (CustomException) {...}

但是我想运行“catch all” block 中使用的代码仍然运行一些与所有 catch block 相关的其他代码......

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
}

或者我是否必须将我想在所有异常情况下做的事情(finally 不是一个选项,因为我希望它只针对异常运行)放入一个单独的方法/嵌套整个try/catch in another (euch)...?

最佳答案

我通常会按照以下方式做一些事情

try
{
throw new CustomException("An exception.");
}
catch (Exception ex)
{
if (ex is CustomException)
{
// Do whatever
}
// Do whatever else
}

关于c# - 对所有捕获重用捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16840130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com