Transfer to different page based on exception type : Exception « Development « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Default"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Exceptions at work</title>
</head>
<body>
    To test this page effectively, first disable the customErrors section in the web.config file and 
                then try again with the section enabled.
    <div id="pageContent">
        <form id="form1" runat="server">
            <ul><li>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Click to throw a NotImplementedException exception</asp:LinkButton> 
            </li><li>
            <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Click to generate an internal error</asp:LinkButton></li>
                <li>
            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="missing.aspx">Click to raise a HTTP 404 error</asp:HyperLink></li>
            </ul>
        </form>
    </div>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Default : System.Web.UI.Page
{
    protected void Page_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();

        if (ex is NotImplementedException)
            Server.Transfer("/notimplementedexception.aspx");
        else
            Server.Transfer("/apperror.aspx");

        Server.ClearError();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
  protected void LinkButton1_Click(object sender, EventArgs e)
  {
    throw new NotImplementedException("The feature you requested is not implemented yet.");
  }
  protected void LinkButton2_Click(object sender, EventArgs e)
  {
        string test = null;
        Response.Write(test.ToString());
  }
}








9.18.Exception
9.18.1.What is going to happen if there is no exception handler
9.18.2.Divide By Zero With Exception
9.18.3.Catch exception and display exception message, Source and StackTrace (C#)
9.18.4.Page-level error handling
9.18.5.Application-level error handling
9.18.6.Try to catch error when converting text value to number
9.18.7.Use Global.asax to log application level exception
9.18.8.Generic error handler page
9.18.9.Transfer to different page based on exception type