次の方法で共有


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

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

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

構文

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

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

パラメータ

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

解説

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

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

使用例

HttpException クラスの HttpException コンストラクタのコード例を次に示します。CheckNumber メソッドは、ユーザーがテキスト ボックスに入力した値を受け取り、それが整数かどうかを確認します。値が整数でない場合、例外がスローされ、続いて catch ブロックで新しい HttpException オブジェクトが作成され、スローされます。この例外は Button_Click イベント ハンドラでキャッチされ、エラー メッセージがブラウザに表示されます。

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

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

<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 an integer.
               Dim convertInt As [String] = textbox1.Text
               Convert.ToInt32(convertInt)
            Catch e As Exception
               ' Throw an HttpException object with a message.
               Throw New HttpException("The value entered in the textbox is not a integer", e)
            End Try
         End Sub 'CheckNumber
       
         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.Text = "<font color='red'>An HttpException was raised!: " + exp.Message + "</font>"
               Dim myInnerException As Exception = exp.InnerException
               label2.Text = "InnerException is : " + myInnerException.GetType().ToString()
            End Try
         End Sub 'Button_Click
       
         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>
<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 e)
            {
               // Throw an HttpException object with a message.
               throw new HttpException("THe value entered in the text box is not a integer", e);
            }
         }
      
         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.Text = "<font color='red'>An HttpException was raised: " + exp.Message + "</font>";
               Exception myInnerException = exp.InnerException;
               label2.Text = "InnerException is : " + myInnerException.GetType();
            }
         }

         void page_load(Object sender,EventArgs e)
         {
            label1.Text="";
            label2.Text="";
         }
      </script>
   </head>

   <body>
      <center>
         <h3>Example for HttpException</h3>
      </center>
      <form id="Form1" method="post" runat="server">
         <center>
            <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>
         </center>
      </form>
   </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

その他の技術情報

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