gpt4 book ai didi

c# - 抛出新异常未被 Server.GetLastError() 捕获

转载 作者:行者123 更新时间:2023-11-30 20:17:39 24 4
gpt4 key购买 nike

在我的 Controller 操作中,我捕获了一个异常并在冒泡到 Application_Error() 方法之前添加了一些额外的信息

ActionResult Index()
{
try
{
var i = 1 / 0;
}
catch(Exception e)
{
throw new Exception("My new Exception!", e)
}
}


private void Application_Error(object sender, EventArgs e)
{
// Breakpoint shows original error! Not my newly thrown one!
var exception = Server.GetLastError().GetBaseException();
}

最佳答案

快速浏览 Exception.GetBaseException Method 的文档清除一切:

A chain of exceptions consists of a set of exceptions such that each exception in the chain was thrown as a direct result of the exception referenced in its InnerException property. For a given chain, there can be exactly one exception that is the root cause of all other exceptions in the chain. This exception is called the base exception and its InnerException property always contains a null reference.

您将原始Exception设置为抛出的ExceptionInnerException,因此原始Exception “基础异常”。

如果您想查看链中最近的 Exception,也许您不应该调用 GetBaseException

关于c# - 抛出新异常未被 Server.GetLastError() 捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44170899/

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