gpt4 book ai didi

c# - 找出具体异常

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

抱歉缺少信息...

有没有办法找出导致调用抛出异常的特定异常是什么?

目前,我正在通过执行 catch(Exception e) 来捕获所有异常,但我知道这根本不是好的做法。

有没有办法从异常中知道导致问题的具体异常是什么,以便我可以更细化?

最佳答案

您可以使用不同的方法获取特定异常的更多信息。在这里,我使用 System.Diagnostics.Debug 类在 Visual Studio 的输出控制台中进行打印。在你的 catch block 中:

using System.Diagnostics;

try
{
// do stuff
}
catch (Exception e)
{
Debug.WriteLine(e.GetType()); // Displays the type of exception
Debug.WriteLine(e.Message()); // Displays the exception message
}

您可以在此处查看更多信息:Exception methods & Exception properties

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

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