gpt4 book ai didi

c# - 异常转换

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:13 25 4
gpt4 key购买 nike

我正在根据异常类型插入其他消息。

我想根据异常类型将不同的自定义消息插入异常表。我不能将switch语句与异常对象一起使用。

关于如何执行此操作的任何建议?

private void ExceptionEngine(Exception e)
{
if (e.)
{
exceptionTable.Rows.Add(null, e.GetType().ToString(), e.Message);
}

最佳答案

if (e is NullReferenceException)
{
...
}
else if (e is ArgumentNullException)
{
...
}
else if (e is SomeCustomException)
{
...
}
else
{
...
}


在这些 if子句中,可以将 e强制转换为相应的异常类型,以检索此异常的某些特定属性: ((SomeCustomException)e).SomeCustomProperty

关于c# - 异常转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5847741/

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