I saw a post today about using the global application class to help get exception information, using server.Transfer('''') to specify the page that accepts errors; plus using server.GetLastError() in the accepting error page to get The previous exception source.
The Application_Error function in Global.asax is as follows:
- protected void Application_Error(object sender, EventArgs e)
- {
- // Catch all exceptions under the entire solution
- try
- {
- Server.Transfer("~/Error.aspx");
- }
- catch { }
- }
The relevant code of the error acceptance page Error.aspx to obtain the exception information is as follows:
- Exception ex = Server.GetLastError().GetBaseException(); //Get exception source
- if (ex != null)
- {
- Response.Write(ex.Message);
- }
- // clear the previous exception
- Server.ClearError();
The test exception code in the test page Text.aspx is as follows:
- //Test whether the exception information is caught
- //test1
- //int UserID = Convert.ToInt32(Request["UserID"].ToString());
- // test2
- string Name = "aganar";
- int UID = Convert.ToInt32(Name);