由Response.Redirect引发的"Thread was being aborted. "异常的处理方法

本文探讨了在使用Response.Redirect时出现的Thread was being aborted.异常问题,并提供了两种解决方案:一是通过判断异常类型忽略特定异常;二是调整Redirect方法的参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将Response.Redirect写入try...catch会出现异常
try 

    Response.Redirect(
"Index.aspx"); 

catch(Exception e) 

    Response.Redirect(
"Error.aspx?message=" + e.Message); 

如上,则会显示"Thread was being aborted. "异常

同样的还有Response.End()等提前结束当前Theard的方法

解决方案一:

如果 Response.Redirect("Index.aspx"); 必须写到Try{}里,则可以在Catch语句写入:

catch(Exception e) 

    
if (!(e is ThreadAbortException)) 
    { 
        Response.Redirect(
"Error.aspx?message=" + e.Message); 
    } 
}
  略过系统对这个特殊异常的处理。

 解决方案二:

MSDN已经解析清楚了
“调用 Redirect 等效于在将第二个参数设置为 true 的情况下调用 Redirect。 Redirect 调用 End,它在完成时引发 ThreadAbortException 异常。” 可见Redirect方法在内部是调用 Thread.Abort()来中止线程的从而引发ThreadAbortException 异常。如果不想立刻中止则,第二个参数设置为false

C# code

protected void Button1_Click(object sender, EventArgs e)
{
try
{
Response.Redirect(
"A.aspx",false);
}
catch( Exception e1)
{
Response.Redirect(
"B.aspx");
}
}

 From:

http://topic.youkuaiyun.com/u/20080909/10/fa572c74-5f5b-4996-a649-36691fcfadcb.html

http://www.zxbc.cn/html/20071128/29848.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值