gpt4 book ai didi

c# - 如何获取 InnerException 的详细信息

转载 作者:太空宇宙 更新时间:2023-11-03 18:03:54 24 4
gpt4 key购买 nike

Regarding the duplicated. I can access the Message property but not the Detail property even when I can see is part of the Exception object during debuging. So the question is Why cant access Detail property.

我发现了一个异常。

catch (Exception ex)
{
string msg = ex.InnerException.InnerException.Message;
// say Exception doesnt have Detail property
// string detail = ex.InnerException.InnerException.Detail;
return Json(new { status = "Fail", message = msg, detail: detail });
}

ex 不说话 enter image description here

ex.InnerException 显示相同的信息 enter image description here

ex.InnerException.InnerException. 终于有一些真正的消息,“db table duplicated key” enter image description here

ex.InnerException.InnerException.Message 我可以收到消息。 enter image description here

但即使只有一个属性 Detail

也无法获取 Detail "the guilty key"

enter image description here

  1. 那么我怎样才能得到详细信息呢?
  2. 奖励:为什么必须深入 InnerException 两次才能获得一些有意义的消息?

最佳答案

我认为现在最优雅的方法是使用 C# 6 when keyword in a catch statementC# 7 is operator .

try
{
//your code
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException pex)
{
string msg = pex.Message;
string detail = pex.Detail;
return Json(new { status = "Fail", message = msg, detail: detail });
}

关于c# - 如何获取 InnerException 的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40249344/

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