HttpException 类

定义

描述在处理 HTTP 请求期间发生的异常。

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

示例

下面的代码示例演示如何使用 HttpException 类引发自定义异常。 方法 Button_Click 确定单击了哪个按钮,然后调用 CheckNumberCheckBoolean 方法。 如果用户在文本框中输入的值与预期类型不对应,则会创建一个 HttpException 异常,并根据错误类型使用自定义消息引发异常。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 能够生成异常信息。 有关引发和处理异常的详细信息,请参阅 异常

构造函数

HttpException()

初始化 HttpException 类的新实例并创建一个空 HttpException 对象。

HttpException(Int32, String)

使用 HTTP 响应状态代码和错误消息初始化 HttpException 类的新实例。

HttpException(Int32, String, Exception)

使用 HTTP 响应状态代码、错误消息以及 HttpException 属性初始化 InnerException 类的新实例。

HttpException(Int32, String, Int32)

使用 HTTP 响应状态代码、错误消息和异常代码初始化 HttpException 类的新实例。

HttpException(SerializationInfo, StreamingContext)

用序列化数据初始化 HttpException 类的新实例。

HttpException(String)

使用所提供的错误消息初始化 HttpException 类的新实例。

HttpException(String, Exception)

使用错误消息和 HttpException 属性初始化 InnerException 类的新实例。

HttpException(String, Int32)

使用错误消息和异常代码初始化 HttpException 类的新实例。

属性

Data

获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。

(继承自 Exception)
ErrorCode

获取错误的 HRESULT

(继承自 ExternalException)
HelpLink

获取或设置指向与此异常关联的帮助文件链接。

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。

(继承自 Exception)
Message

获取描述当前异常的消息。

(继承自 Exception)
Source

获取或设置导致错误的应用程序或对象的名称。

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。

(继承自 Exception)
TargetSite

获取引发当前异常的方法。

(继承自 Exception)
WebEventCode

获取与 HTTP 异常相关联的事件代码。

方法

CreateFromLastError(String)

基于从 Windows API GetLastError() 方法返回的错误代码创建一个新的 HttpException 异常。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。

(继承自 Exception)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetHtmlErrorMessage()

获取要返回给客户端的 HTML 错误消息。

GetHttpCode()

获取要返回给客户端的 HTTP 响应状态代码。

GetObjectData(SerializationInfo, StreamingContext)

获取关于异常的信息并将其添加到 SerializationInfo 对象。

GetObjectData(SerializationInfo, StreamingContext)

当在派生类中重写时,用关于异常的信息设置 SerializationInfo

(继承自 Exception)
GetType()

获取当前实例的运行时类型。

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回一个字符串,该字符串包含错误的 HRESULT。

(继承自 ExternalException)

事件

SerializeObjectState
已过时.

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。

(继承自 Exception)

适用于

另请参阅