关于重复。我可以访问该
Message属性,但不能访问该属性,Detail即使在调试期间我可以看到它是 Exception 对象的一部分。所以问题是为什么不能访问Detail财产。
我捕捉到一个异常。
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 });
}
Run Code Online (Sandbox Code Playgroud)
例如.InnerException.InnerException。终于有一些真实的消息,"db table duplicated key"

ex.InnerException.InnerException.Message我可以得到消息。

但是"the guilty key"即使有一个属性也无法获得详细信息Detail
InnerException两次才能获得一些有意义的信息?我认为现在最优雅的方法是在 catch 语句中使用C# 6 关键字,而C# 7 是 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 });
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7965 次 |
| 最近记录: |