Here is sample code how you can display exceptions on custom page.
First create Default.aspx with button:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Throw Error" />
Add following code for button click event:
protected void Button1_Click(object sender, EventArgs e)
{
throw new Exception("Sample Exception on my Page");
}
Second create ErrorPage.aspx with label:
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
And code for error page:
protected void Page_Load(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex != null && ex.InnerException != null)
{
Label1.Text = string.Format("An error occured: {0}", ex.InnerException.Message);
}
}
And finally place following configuration in web.config:
<system.web>
<customErrors mode="On" defaultRedirect="~/ErrorPage.aspx" redirectMode="ResponseRewrite" />
</system.web>
Compile and start with Default.aspx. Click your button and error will be shown on your custom page.
Happy coding!