HttpException HttpException HttpException HttpException Class

定义

描述在处理 HTTP 请求期间发生的异常。Describes an exception that occurred during the processing of HTTP requests.

public ref class HttpException : System::Runtime::InteropServices::ExternalException
[System.Serializable]
public class HttpException : System.Runtime.InteropServices.ExternalException
type HttpException = class
    inherit ExternalException
Public Class HttpException
Inherits ExternalException
继承
派生
属性

示例

下面的代码示例演示如何使用HttpException类引发自定义异常。The following code example demonstrates how to use the HttpException class to raise customized exceptions. 方法确定单击了哪个按钮, 然后调用CheckNumberCheckBoolean方法。 Button_ClickThe Button_Click method determines which button was clicked, and then calls either the CheckNumber or CheckBoolean method. 如果文本框中的用户输入值与所需类型不对应, HttpException则会基于错误类型创建并使用自定义消息引发异常。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.

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。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>
<%@ Page Language="VB" %>

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

<script runat="server">
  Sub CheckNumber()
    
    Try

      ' Check whether the value is an integer.
      Dim convertInt As String = textbox1.Text
      Convert.ToInt32(convertInt)

    Catch e As Exception

      ' Throw an HttpException with customized message.
      Throw New HttpException("not an integer")

    End Try

  End Sub

  Sub CheckBoolean()
    
    Try

      ' Check whether the value is an boolean.
      Dim convertBool As String = textbox1.Text
      Convert.ToBoolean(convertBool)

    Catch e As Exception

      ' Throw an HttpException with customized message.
      Throw New HttpException("not a boolean")

    End Try

  End Sub

  Sub Button_Click(ByVal sender As [Object], ByVal e As EventArgs)
    
    Try

      ' Check to see which button was clicked.
      Dim b As Button = CType(sender, Button)
      If b.ID.StartsWith("button1") Then
        CheckNumber()
      ElseIf b.ID.StartsWith("button2") Then
        CheckBoolean()
      End If

      label1.Text = "You entered: " + textbox1.Text
      label1.ForeColor = System.Drawing.Color.Black

      ' Catch the HttpException.
    Catch exp As HttpException

      label1.Text = "An HttpException was raised. " + "The value entered in the textbox is " + exp.Message.ToString()
      label1.ForeColor = System.Drawing.Color.Red

    End Try

  End Sub
</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>

注解

HttpException类是 HTTP 特定的异常类, 它使 ASP.NET 能够生成异常信息。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 Exceptions.

构造函数

HttpException() HttpException() HttpException() HttpException()

初始化 HttpException 类的新实例并创建一个空 HttpException 对象。Initializes a new instance of the HttpException class and creates an empty HttpException object.

HttpException(Int32, String) HttpException(Int32, String) HttpException(Int32, String) HttpException(Int32, String)

使用 HTTP 响应状态代码和错误消息初始化 HttpException 类的新实例。Initializes a new instance of the HttpException class using an HTTP response status code and an error message.

HttpException(Int32, String, Exception) HttpException(Int32, String, Exception) HttpException(Int32, String, Exception) HttpException(Int32, String, Exception)

使用 HTTP 响应状态代码、错误消息以及 HttpException 属性初始化 InnerException 类的新实例。Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and the InnerException property.

HttpException(Int32, String, Int32) HttpException(Int32, String, Int32) HttpException(Int32, String, Int32) HttpException(Int32, String, Int32)

使用 HTTP 响应状态代码、错误消息和异常代码初始化 HttpException 类的新实例。Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and an exception code.

HttpException(SerializationInfo, StreamingContext) HttpException(SerializationInfo, StreamingContext) HttpException(SerializationInfo, StreamingContext) HttpException(SerializationInfo, StreamingContext)

用序列化数据初始化 HttpException 类的新实例。Initializes a new instance of the HttpException class with serialized data.

HttpException(String) HttpException(String) HttpException(String) HttpException(String)

使用所提供的错误消息初始化 HttpException 类的新实例。Initializes a new instance of the HttpException class using a supplied error message.

HttpException(String, Exception) HttpException(String, Exception) HttpException(String, Exception) HttpException(String, Exception)

使用错误消息和 HttpException 属性初始化 InnerException 类的新实例。Initializes a new instance of the HttpException class using an error message and the InnerException property.

HttpException(String, Int32) HttpException(String, Int32) HttpException(String, Int32) HttpException(String, Int32)

使用错误消息和异常代码初始化 HttpException 类的新实例。Initializes a new instance of the HttpException class using an error message and an exception code.

属性

Data Data Data Data

获取提供有关异常的其他用户定义信息的键/值对集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
ErrorCode ErrorCode ErrorCode ErrorCode

获取错误的 HRESULTGets the HRESULT of the error.

(Inherited from ExternalException)
HelpLink HelpLink HelpLink HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

获取描述当前异常的消息。Gets a message that describes the current exception.

(Inherited from Exception)
Source Source Source Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(Inherited from Exception)
WebEventCode WebEventCode WebEventCode WebEventCode

获取与 HTTP 异常相关联的事件代码。Gets the event codes that are associated with the HTTP exception.

方法

CreateFromLastError(String) CreateFromLastError(String) CreateFromLastError(String) CreateFromLastError(String)

基于从 Windows API GetLastError() 方法返回的错误代码创建一个新的 HttpException 异常。Creates a new HttpException exception based on the error code that is returned from the Windows API GetLastError() method.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetHtmlErrorMessage() GetHtmlErrorMessage() GetHtmlErrorMessage() GetHtmlErrorMessage()

获取要返回给客户端的 HTML 错误消息。Gets the HTML error message to return to the client.

GetHttpCode() GetHttpCode() GetHttpCode() GetHttpCode()

获取要返回给客户端的 HTTP 响应状态代码。Gets the HTTP response status code to return to the client.

GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

获取关于异常的信息并将其添加到 SerializationInfo 对象。Gets information about the exception and adds it to the SerializationInfo object.

GetType() GetType() GetType() GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回一个字符串,该字符串包含错误的 HRESULT。Returns a string that contains the HRESULT of the error.

(Inherited from ExternalException)

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

适用于

另请参阅