HttpException Class
TOC
Collapse the table of content
Expand the table of content

HttpException Class

 

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

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

System.Object
  System.Exception
    System.SystemException
      System.Runtime.InteropServices.ExternalException
        System.Web.HttpException
          System.Web.HttpCompileException
          System.Web.HttpParseException
          System.Web.HttpRequestValidationException
          System.Web.HttpUnhandledException

[SerializableAttribute]
public class HttpException : ExternalException

NameDescription
System_CAPS_pubmethodHttpException()

Initializes a new instance of the HttpException class and creates an empty HttpException object.

System_CAPS_pubmethodHttpException(Int32, String)

Initializes a new instance of the HttpException class using an HTTP response status code and an error message.

System_CAPS_pubmethodHttpException(Int32, String, Exception)

Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and the InnerException property.

System_CAPS_pubmethodHttpException(Int32, String, Int32)

Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and an exception code.

System_CAPS_protmethodHttpException(SerializationInfo, StreamingContext)

Initializes a new instance of the HttpException class with serialized data.

System_CAPS_pubmethodHttpException(String)

Initializes a new instance of the HttpException class using a supplied error message.

System_CAPS_pubmethodHttpException(String, Exception)

Initializes a new instance of the HttpException class using an error message and the InnerException property.

System_CAPS_pubmethodHttpException(String, Int32)

Initializes a new instance of the HttpException class using an error message and an exception code.

NameDescription
System_CAPS_pubpropertyData

Gets a collection of key/value pairs that provide additional user-defined information about the exception.(Inherited from Exception.)

System_CAPS_pubpropertyErrorCode

Gets the HRESULT of the error.(Inherited from ExternalException.)

System_CAPS_pubpropertyHelpLink

Gets or sets a link to the help file associated with this exception.(Inherited from Exception.)

System_CAPS_pubpropertyHResult

Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.(Inherited from Exception.)

System_CAPS_pubpropertyInnerException

Gets the Exception instance that caused the current exception.(Inherited from Exception.)

System_CAPS_pubpropertyMessage

Gets a message that describes the current exception.(Inherited from Exception.)

System_CAPS_pubpropertySource

Gets or sets the name of the application or the object that causes the error.(Inherited from Exception.)

System_CAPS_pubpropertyStackTrace

Gets a string representation of the immediate frames on the call stack.(Inherited from Exception.)

System_CAPS_pubpropertyTargetSite

Gets the method that throws the current exception.(Inherited from Exception.)

System_CAPS_pubpropertyWebEventCode

Gets the event codes that are associated with the HTTP exception.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticCreateFromLastError(String)

Creates a new HttpException exception based on the error code that is returned from the Win32 API GetLastError() method.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetBaseException()

When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.(Inherited from Exception.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetHtmlErrorMessage()

Gets the HTML error message to return to the client.

System_CAPS_pubmethodGetHttpCode()

Gets the HTTP response status code to return to the client.

System_CAPS_pubmethodGetObjectData(SerializationInfo, StreamingContext)

Gets information about the exception and adds it to the SerializationInfo object. (Overrides Exception.GetObjectData(SerializationInfo, StreamingContext).)

System_CAPS_pubmethodGetType()

Gets the runtime type of the current instance.(Inherited from Exception.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that contains the HRESULT of the error.(Inherited from ExternalException.)

NameDescription
System_CAPS_proteventSerializeObjectState

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

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.

System_CAPS_security Security 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
Available since 1.1

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

Return to top
Show:
© 2016 Microsoft