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 メソッドは、クリックされたボタンを決定し、CheckNumber または CheckBoolean メソッドを呼び出します。 テキスト ボックスにユーザーが入力した値が想定される型に対応していない場合は、エラーの種類に基づいてカスタマイズされたメッセージを含む HttpException 例外が作成され、スローされます。

Important

この例には、潜在的なセキュリティ上の脅威であるユーザー入力を受け入れるテキスト ボックスがあります。 既定では、ASP.NET Web ページでは、ユーザー入力にスクリプトや 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 クラスは、ASP.NET が例外情報を生成できるようにする HTTP 固有の例外クラスです。 例外のスローと処理の詳細については、「 例外」を参照してください。

コンストラクター

名前 説明
HttpException()

HttpException クラスの新しいインスタンスを初期化し、空のHttpException オブジェクトを作成します。

HttpException(Int32, String, Exception)

HTTP 応答状態コード、エラー メッセージ、および HttpException プロパティを使用して、InnerException クラスの新しいインスタンスを初期化します。

HttpException(Int32, String, Int32)

HTTP 応答状態コード、エラー メッセージ、および例外コードを使用して、 HttpException クラスの新しいインスタンスを初期化します。

HttpException(Int32, String)

HTTP 応答状態コードとエラー メッセージを使用して、 HttpException クラスの新しいインスタンスを初期化します。

HttpException(SerializationInfo, StreamingContext)

シリアル化されたデータを使用して、 HttpException クラスの新しいインスタンスを初期化します。

HttpException(String, Exception)

エラー メッセージと HttpException プロパティを使用して、InnerException クラスの新しいインスタンスを初期化します。

HttpException(String, Int32)

エラー メッセージと例外コードを使用して、 HttpException クラスの新しいインスタンスを初期化します。

HttpException(String)

指定されたエラー メッセージを使用して、 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()

派生クラスでオーバーライドされた場合、1 つ以上の後続の例外の根本原因である 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)

適用対象

こちらもご覧ください