(1) The approach u are following is right. You need to specify different error pages for different types of errors (if you want to handle different error types in different ways)like mentioned by nadeem,
(2) 2nd approach preferable is using Application_Error in Global.asax like below:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Get the exception object.
Exception exc = Server.GetLastError();
// For other kinds of errors give the user some information
// but stay on the default page
Response.Write("<h2>Global Page Error</h2>\n");
Response.Write(
"<p>" + exc.Message + "</p>\n");
Response.Write("Return to the <a href='Default.aspx'>" +
"Default Page</a>\n");
// Clear the error from the server
Server.ClearError();
}
(3) 3rd approach is use of Application_Error and custom error pages together. using Application_Error for getting exception details and passing it to the error page like,
protected void Application_Error(object sender, EventArgs e)
{
Exception err = Server.GetLastError();
Session.Add("LastError", err);
}
void Session_Start(object sender, EventArgs e)
{
Session["LastError"] = ""; //initialize the session
}
In error page :
protected void Page_Load(object sender, EventArgs e)
{
Exception err = Session["LastError"] as Exception;
//Exception err = Server.GetLastError();
if (err != null)
{
err = err.GetBaseException();
lblErrorMsg.Text = err.Message;
lblSource.Text = err.Source;
lblInnerEx.Text = (err.InnerException != null) ? err.InnerException.ToString() : "";
lblStackTrace.Text = err.StackTrace;
Session["LastError"] = null;
}
}