Export (0) Print
Expand All

HttpException Class

Describes an exception that occurred during the processing of HTTP requests.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

[SerializableAttribute]
public class HttpException : ExternalException

The HttpException type exposes the following members.

  NameDescription
Public methodHttpException()Initializes a new instance of the HttpException class and creates an empty HttpException object.
Public methodHttpException(String)Initializes a new instance of the HttpException class using a supplied error message.
Public methodHttpException(Int32, String)Initializes a new instance of the HttpException class using an HTTP response status code and an error message.
Protected methodHttpException(SerializationInfo, StreamingContext)Initializes a new instance of the HttpException class with serialized data.
Public methodHttpException(String, Exception)Initializes a new instance of the HttpException class using an error message and the InnerException property.
Public methodHttpException(String, Int32)Initializes a new instance of the HttpException class using an error message and an exception code.
Public methodHttpException(Int32, String, Exception)Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and the InnerException property.
Public methodHttpException(Int32, String, Int32)Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and an exception code.
Top

  NameDescription
Public propertyDataGets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)
Public propertyErrorCodeGets the HRESULT of the error. (Inherited from ExternalException.)
Public propertyHelpLinkGets or sets a link to the help file associated with this exception. (Inherited from Exception.)
Public propertyHResultGets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)
Public propertyInnerExceptionGets the Exception instance that caused the current exception. (Inherited from Exception.)
Public propertyMessageGets a message that describes the current exception. (Inherited from Exception.)
Public propertySourceGets or sets the name of the application or the object that causes the error. (Inherited from Exception.)
Public propertyStackTraceGets a string representation of the immediate frames on the call stack. (Inherited from Exception.)
Public propertyTargetSiteGets the method that throws the current exception. (Inherited from Exception.)
Public propertyWebEventCodeGets the event codes that are associated with the HTTP exception.
Top

  NameDescription
Public methodStatic memberCreateFromLastErrorCreates a new HttpException exception based on the error code that is returned from the Win32 API GetLastError() method.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetBaseExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetHtmlErrorMessageGets the HTML error message to return to the client.
Public methodGetHttpCodeGets the HTTP response status code to return to the client.
Public methodGetObjectDataGets information about the exception and adds it to the SerializationInfo object. (Overrides Exception.GetObjectData(SerializationInfo, StreamingContext).)
Public methodGetTypeGets the runtime type of the current instance. (Inherited from Exception.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that contains the HRESULT of the error. (Inherited from ExternalException.)
Top

  NameDescription
Protected eventSerializeObjectStateOccurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception.)
Top

The HttpException class is an HTTP-specific exception class that enables ASP.NET to generate exception information. For more information about throwing and handling exceptions, see Handling and Throwing Exceptions.

The following code example demonstrates how to use the HttpException class to raise customized exceptions. The Button_Click method determines which button was clicked, and then calls either the CheckNumber or CheckBoolean method. If the user-entered value in the text box does not correspond to the expected type, an HttpException exception is created and thrown with a customized message based on the error type.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    void CheckNumber()
    {
        try
        {
            // Check whether the value is an integer.
            String convertInt = textbox1.Text;
            Convert.ToInt32(convertInt);
        }
        catch (Exception e)
        {
            // Throw an HttpException with customized message.
            throw new HttpException("not an integer");
        }
    }
    void CheckBoolean()
    {
        try
        {
            // Check whether the value is an boolean.
            String convertBool = textbox1.Text;
            Convert.ToBoolean(convertBool);
        }
        catch (Exception e)
        {
            // Throw an HttpException with customized message.
            throw new HttpException("not a boolean");
        }
    }

    void Button_Click(Object sender, EventArgs e)
    {
        try
        {
            // Check to see which button was clicked.
            Button b = (Button)sender;
            if (b.ID.StartsWith("button1"))
                CheckNumber();
            else if (b.ID.StartsWith("button2"))
                CheckBoolean();

            label1.Text = "You entered: " + textbox1.Text;
            label1.ForeColor = System.Drawing.Color.Black;
        }
        // Catch the HttpException.
        catch (HttpException exp)
        {
            label1.Text = "An HttpException was raised. "
               + "The value entered in the textbox is " + exp.Message.ToString();
            label1.ForeColor = System.Drawing.Color.Red;
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpException Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <b>Enter a value in the text box.</b>
        <br />
        <asp:TextBox ID="textbox1" 
                     Runat="server">
        </asp:TextBox>
        <br />
        <asp:Button ID="button1"
                    Text="Check for integer."  
                    OnClick="Button_Click" 
                    Runat="server">
        </asp:Button>
        <br />
        <asp:Button ID="button2"
                    Text="Check for boolean." 
                    OnClick="Button_Click" 
                    Runat="server">
        </asp:Button>
        <br />
        <asp:Label ID="label1" 
                   Runat="server">
        </asp:Label>    
    </div>
    </form>
</body>
</html>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft