问题描述
在下面的code,有时someFunctionCall()生成一个线程已被中止的异常。为什么在code code块B从不运行? ASP.NET是否开始为每个方法调用一个新的线程?我很惊讶地看到,当这个例外发生在code在B座从来没有运行,该方法返回,和我的应用程序保持运行。有人可以解释一下吗?
感谢。
公共无效的方法()
{
// code座一
// ...
尝试
{
someFunctionCall(); //这个调用产生线程中止异常
}
赶上(例外前)
{
//日志异常消息
}
// code B座
// ...
}
这是一个 ThreadAbortException
;这是一个特殊的例外,即在每一个catch块结束时会自动重新抛出,除非你叫 Thread.ResetAbort()
。
ASP.NET的方法,如到Response.End
或的Response.Redirect
(除非你通过假
)抛出此异常来结束当前页的处理;你的 someFunctionCall()
可能是调用这些方法之一。
ASP.NET的自己处理这个异常并调用 ResetAbort
继续处理。
In the code below, sometimes someFunctionCall() generates a "Thread was being aborted" exception. How come the code in code block B never runs? Does ASP.NET start a new thread for each method call? I was suprised to see that when this exception happens the code in block b never runs, the method returns, and my application keeps running. Can someone please explain this?
Thanks.
public void method()
{
// CODE BLOCK A
//...
try
{
someFunctionCall(); // this call is generating thread abort exception
}
catch(Exception ex)
{
// log exception message
}
// CODE BLOCK B
// ...
}
This is a ThreadAbortException
; it's a special exception that is automatically rethrown at the end of every catch block, unless you call Thread.ResetAbort()
.
ASP .Net methods like Response.End
or Response.Redirect
(unless you pass false
) throw this exception to end processing of the current page; your someFunctionCall()
is probably calling one of those methods.
ASP .Net itself handles this exception and calls ResetAbort
to continue processing.
这篇关于ASP.NET例外"正在中止线程"导致方法退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!