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. Button_Click方法會判斷哪一個按鈕已按下,,然後再呼叫其中一個CheckNumberCheckBoolean方法。The 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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。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類別是可讓 ASP.NET 產生的例外狀況資訊的 HTTP 特定的例外狀況類別。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()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen 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 的淺層複本 (Shallow Copy)。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)

適用於

另請參閱