次の方法で共有


HttpException コンストラクタ (Int32, String, Exception)

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

名前空間: System.Web
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Sub New ( _
    httpCode As Integer, _
    message As String, _
    innerException As Exception _
)
'使用
Dim httpCode As Integer
Dim message As String
Dim innerException As Exception

Dim instance As New HttpException(httpCode, message, innerException)
public HttpException (
    int httpCode,
    string message,
    Exception innerException
)
public:
HttpException (
    int httpCode, 
    String^ message, 
    Exception^ innerException
)
public HttpException (
    int httpCode, 
    String message, 
    Exception innerException
)
public function HttpException (
    httpCode : int, 
    message : String, 
    innerException : Exception
)
適用できません。

パラメータ

  • httpCode
    クライアントに表示する HTTP 応答ステータス コード。
  • message
    例外がスローされたときに、クライアントに表示するエラー メッセージ。
  • innerException
    存在する場合は、現在の例外をスローした InnerException

解説

例外を処理する場合は、内部例外が原因でスローされた外部例外と一緒に、関連する一連の例外についても情報を収集すると役に立つことがあります。

外部例外を引き起した内部例外への参照は、その外部例外の InnerException プロパティで取得できます。この機構では、以前の例外で取得されたエラー情報 (原因となった例外など) が保持されます。また、よりわかりやすい外部例外を作成することもできます。詳細については、InnerException のトピックを参照してください。

使用例

HttpException クラスの HttpException コンストラクタのコード例を次に示します。CheckNumber メソッドは、ユーザーが入力した値を受け取り、それが整数かどうかを確認します。値が整数でない場合、例外がスローされ、HTTP 応答ステータス コード、例外のメッセージ、および内部例外 (ある場合) を含む新しい HttpException オブジェクトが作成されます。この例外は Button_Click イベント ハンドラでキャッチされ、エラー メッセージ、エラー コード、および内部例外が表示されます。

セキュリティに関するメモセキュリティに関するメモ :

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは HTML 要素が含まれていないかどうかが検証されます。詳細については、「スクリプトによる攻略の概要」を参照してください。

<%@ Import Namespace="System.Drawing" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>Example for HttpException</title>
<script language="VB" runat="server">
         Sub CheckNumber()
            Try
               'Check whether the value is integer.
               Dim convertInt As [String] = textbox1.Text
               Convert.ToInt32(convertInt)
            Catch ex As Exception
               ' Throw an HttpException object that contains the HTTP error code,
               ' message, and inner exception.
               Throw New HttpException(500, "The entered value is not an integer.", ex)
            End Try
         End Sub
 
         Sub Button_Click(sender As [Object], e As EventArgs)
            Try
               CheckNumber()
               label1.Text = "The integer Value you entered is: " & textbox1.Text

            Catch exp As HttpException
               ' Display the Exception thrown.
               label1.ForeColor = Color.Red
               label1.Text = "An HttpException was raised: " & exp.Message

               Dim myInnerException As Exception = exp.InnerException

               ' Display the inner exception.
               label2.Text = "InnerException is : " & myInnerException.GetType().ToString()
                
            End Try
         End Sub 

         Sub page_load(sender As [Object], e As EventArgs)
            label1.Text = ""
            label2.Text = ""
         End Sub 
      </script>
   </head>

   <body>
      <center>
         <h3>Example for HttpException</h3>
         <form id="WebForm9" method="post" runat="server">
            <b>Enter the value in the text box.</b>
            <asp:TextBox Runat="server" ID="textbox1"></asp:TextBox>
            <br />
            <asp:Button Text="Click Here" OnClick="Button_Click" Runat="server" ID="Button1"></asp:Button>
            <br />
            <b>
            <asp:Label Runat="server" ID="label1"></asp:Label>
            <br />
            <asp:Label Runat="server" ID="label2"></asp:Label>
            </b>
         </form>
      </center>
   </body>
</html>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>Example for HttpException</title>
<script language="C#" runat="server">
         void CheckNumber()
         {
            try
            {
               // Check whether the value is an integer.
               String convertInt = textbox1.Text;
               Convert.ToInt32(convertInt);
            }
            catch(Exception ex)
            {
               // Throw an HttpException object that contains the HTTP error code,
               // message, and inner exception.
               throw new HttpException(500, "The entered value is not an integer.", ex);
            }
         }
         
         void Button_Click(Object sender, EventArgs e)
         {
            try
            {
               CheckNumber();
               label1.Text = "The integer value you entered is: " + textbox1.Text;
            }
            catch(HttpException exp)
            {
               // Display the exception thrown.
               label1.ForeColor = Color.Red;
               label1.Text = "An HttpException was raised!: " + exp.Message;
               Exception myInnerException = exp.InnerException;
               
               // Display the inner exception.
               label2.Text = "The InnerException is : " + myInnerException.GetType();
                
            }
         }
     
         void page_load(Object sender,EventArgs e)
         {
           label1.Text="";
           label2.Text="";
         }

      </script>
   </head>

   <body>
      <center>
         <h3>Example for HttpException</h3>
         <form id="WebForm9" method="post" runat="server">
            <b>Enter the value in the text box </b>
            <br />
            <asp:TextBox Runat="server" ID="textbox1"></asp:TextBox>
            <br />
            <asp:Button Text="Click Here" OnClick="Button_Click" Runat="server" ID="Button1"></asp:Button>
            <br />
            <b>
               <asp:Label Runat="server" ID="label1"></asp:Label>
               <br />
               <asp:Label Runat="server" ID="label2"></asp:Label>
            </b>
         </form>
      </center>
   </body>
</html>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>Example for HttpException</title>
<script language="VJ#" runat="server">
        void CheckNumber() throws HttpException
        {
            try {
                // Check whether the value is an integer.
                String convertInt = textbox1.get_Text();
                Convert.ToInt32(convertInt);
            }
            catch(Exception ex) {
                // Throw an HttpException object that contains the HTTP error 
                // code, message, and inner exception.
                throw new HttpException(500, "The entered value is not an " 
                    + "integer.", ex);
            }
        } //CheckNumber
        
        void Button_Click(Object sender, EventArgs e)
        {
            try {
                CheckNumber();
                label1.set_Text("The integer value you entered is: " 
                    + textbox1.get_Text());
            }
            catch(HttpException exp) {
                // Display the exception thrown.
                label1.set_ForeColor(Color.get_Red());
                label1.set_Text("An HttpException was raised!: " 
                    + exp.get_Message());
                System.Exception myInnerException = exp.get_InnerException();
                
                // Display the inner exception.
                label2.set_Text("The InnerException is : " 
                    + myInnerException.GetType());
            }
        } //Button_Click
     
        void page_load(Object sender,EventArgs e)
        {
            label1.set_Text("");
            label2.set_Text("");
        } //page_load
      </script>
   </head>

   <body>
      <center>
         <h3>Example for HttpException</h3>
         <form id="WebForm9" method="post" runat="server">
            <b>Enter the value in the text box </b>
            <br />
            <asp:TextBox Runat="server" ID="textbox1"></asp:TextBox>
            <br />
            <asp:Button Text="Click Here" OnClick="Button_Click" Runat="server" ID="Button1"></asp:Button>
            <br />
            <b>
               <asp:Label Runat="server" ID="label1"></asp:Label>
               <br />
               <asp:Label Runat="server" ID="label2"></asp:Label>
            </b>
         </form>
      </center>
   </body>
</html>

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

HttpException クラス
HttpException メンバ
System.Web 名前空間
InnerException

その他の技術情報

スクリプトによる攻略の概要