WebApplication開發中通常是需要使用異常捕獲,以提供給用戶良好的提示頁面。
爲了減少代碼,統一日誌處理,可以在 Global.asax 中 Application_Error 的事件處理方法中統一處理。
它將捕獲所有 Application 級別的 UnhandleException 和 HttpException(比如:訪問的頁面不存在等)
總之,在這裏處理的話,那麼在頁面中的所有 try/catch 處理都可以不要了。
void Application_Error( object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
try
{
Server.Transfer( " ~/Error.aspx " );
}
catch
{
// ignore
}
}
{
// Code that runs when an unhandled error occurs
try
{
Server.Transfer( " ~/Error.aspx " );
}
catch
{
// ignore
}
}
因爲 Server.Transfer 將固定拋出 ThreadAbort Exception 異常,不用理會。