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?
在下面的代码中,someFunctionCall()有时会生成“Thread is aborted”异常。为什么代码块B中的代码永远不会运行? ASP.NET是否为每个方法调用启动一个新线程?我很惊讶地看到,当发生此异常时,块b中的代码永远不会运行,该方法返回,并且我的应用程序继续运行。有人可以解释一下吗?
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
// ...
}
3 个解决方案
#1
30
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()
.
这是一个ThreadAbortException;它是一个特殊的异常,它会在每个catch块的末尾自动重新抛出,除非你调用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.
像Response.End或Response.Redirect这样的ASP .Net方法(除非你传递false)将此异常抛给当前页面的结束处理;你的someFunctionCall()可能正在调用其中一种方法。
ASP .Net itself handles this exception and calls ResetAbort
to continue processing.
ASP .Net本身处理此异常并调用ResetAbort继续处理。
#2
1
To work around this problem, use one of the following methods: For Response.End
, call the HttpContext.Current.ApplicationInstance.CompleteRequest
method instead of Response.End
to bypass the code execution to the Application_EndRequest
event.
若要解决此问题,请使用下列方法之一:对于Response.End,请调用HttpContext.Current.ApplicationInstance.CompleteRequest方法而不是Response.End以绕过代码执行到Application_EndRequest事件。
For Response.Redirect
, use an overload, Response.Redirect(String url, bool endResponse)
that passes false for the endResponse
parameter to suppress the internal call to Response.End
. For example:
对于Response.Redirect,使用一个重载,Response.Redirect(String url,bool endResponse),它为endResponse参数传递false以禁止对Response.End的内部调用。例如:
Response.Redirect ("nextpage.aspx", false);
If you use this workaround, the code that follows Response.Redirect
is executed. For Server.Transfer
, use the Server.Execute
method instead.
如果使用此解决方法,则会执行Response.Redirect之后的代码。对于Server.Transfer,请改用Server.Execute方法。
#3
-2
Try this way :
试试这种方式:
This is my extension method :
这是我的扩展方法:
public static void WriteJSONObject(this HttpResponse response, object content) {
response.ContentType = "application/json";
response.Write(new JavaScriptSerializer().Serialize(content));
response.End();
}
And the logic :
逻辑:
public void RegisterUser() {
try {
Response.WriteJSONObject(new { Result = "See hello" });
}
catch (Exception err) {
if (err.Message != "Thread was being aborted.")
Response.WriteJSONObject(new { Result = err.Message });
else {
Response.End();
}
}
}
#1
30
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()
.
这是一个ThreadAbortException;它是一个特殊的异常,它会在每个catch块的末尾自动重新抛出,除非你调用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.
像Response.End或Response.Redirect这样的ASP .Net方法(除非你传递false)将此异常抛给当前页面的结束处理;你的someFunctionCall()可能正在调用其中一种方法。
ASP .Net itself handles this exception and calls ResetAbort
to continue processing.
ASP .Net本身处理此异常并调用ResetAbort继续处理。
#2
1
To work around this problem, use one of the following methods: For Response.End
, call the HttpContext.Current.ApplicationInstance.CompleteRequest
method instead of Response.End
to bypass the code execution to the Application_EndRequest
event.
若要解决此问题,请使用下列方法之一:对于Response.End,请调用HttpContext.Current.ApplicationInstance.CompleteRequest方法而不是Response.End以绕过代码执行到Application_EndRequest事件。
For Response.Redirect
, use an overload, Response.Redirect(String url, bool endResponse)
that passes false for the endResponse
parameter to suppress the internal call to Response.End
. For example:
对于Response.Redirect,使用一个重载,Response.Redirect(String url,bool endResponse),它为endResponse参数传递false以禁止对Response.End的内部调用。例如:
Response.Redirect ("nextpage.aspx", false);
If you use this workaround, the code that follows Response.Redirect
is executed. For Server.Transfer
, use the Server.Execute
method instead.
如果使用此解决方法,则会执行Response.Redirect之后的代码。对于Server.Transfer,请改用Server.Execute方法。
#3
-2
Try this way :
试试这种方式:
This is my extension method :
这是我的扩展方法:
public static void WriteJSONObject(this HttpResponse response, object content) {
response.ContentType = "application/json";
response.Write(new JavaScriptSerializer().Serialize(content));
response.End();
}
And the logic :
逻辑:
public void RegisterUser() {
try {
Response.WriteJSONObject(new { Result = "See hello" });
}
catch (Exception err) {
if (err.Message != "Thread was being aborted.")
Response.WriteJSONObject(new { Result = err.Message });
else {
Response.End();
}
}
}