为什么Response.Redirect总是在内部抛出一个异常,由框架自动解决 [英] Why Response.Redirect always throws an exception internally which gets resolved automatically by the framework
问题描述
如果我在try块中编写Response.Redirect,我刚遇到 System.Threading.ThreadAbortException
异常。请参阅下面我编写的代码。
I just encountered "System.Threading.ThreadAbortException
" exception if I write Response.Redirect in a try block. See below the code I have written.
protected void btnVisitSecondPage_Click(object sender, EventArgs e)
Response.Redirect("frmSecondPage.aspx");
}
catch (Exception ex) { }
}
上面的代码工作正常,但如果我调试它,我可以看到 Response.Redirect
抛出 System.Threading.ThreadAbortException
异常并由framewrok自动解决。
最奇怪的部分是,如果我删除了try块,那么它也可以正常运行而不会抛出异常。
见下面的代码
Above code is working fine but if I debug it, I can see Response.Redirect
throws "System.Threading.ThreadAbortException
" exception and gets resolved by the framewrok autonmatically.
Most strange part is that if I remove the try block then it also works fine without throwing an exception .
See below the code
protected void btnVisitSecondPage_Click(object sender, EventArgs e)Response.Redirect("frmSecondPage.aspx");
任何人都可以给我解释一下吗?
Can any one please give me the explanation ?
推荐答案
我希望它能解释您的问题。
I hope it explains your problem.
我刚才尝试了一个有2页的新网络应用程序,
I have just tried a new web application with 2 pages,
Page1有一个带有response.redirect到第2页的按钮
Page1 had a button with a response.redirect to page2
Page2有一个带有response.redirect wi的按钮将try1块稀释到page1。
Page2 had a button with a response.redirect within a try block to page1.
它没有任何问题。
Response.Redirect总是抛出一个excpetion。
最好的办法是捕获你想要的异常类型。
Response.Redirect always throws a excpetion.
Best thing to do is catch the type of exception you want.
这篇关于为什么Response.Redirect总是在内部抛出一个异常,由框架自动解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!