次の方法で共有


HttpException コンストラクタ ()

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

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

構文

'宣言
Public Sub New
'使用
Dim instance As New HttpException
public HttpException ()
public:
HttpException ()
public HttpException ()
public function HttpException ()
適用できません。

解説

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

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

使用例

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

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

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

<!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="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 the 'HttpException' object.
               Throw New HttpException()
            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
               label1.Text = "<font color='red'>An HttpException was raised!:" _
                  & " The value entered in the textbox is not an integer</font>"
            End Try
         End Sub 'Button_Click
       
         Sub Page_Load(sender As [Object], e As EventArgs)
            label1.Text=""
         End Sub
      </script>
   </head>

   <body>
      <center>
         <h3>Example for HttpException</h3>
      </center>
      <form id="WebForm9" method="post" runat="server">
         <center>
            <b>Enter a 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"></asp:Button>
            <br />
            <b><asp:Label Runat="server" ID="label1"></asp:Label></b>
         </center>
      </form>
   </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 a 'HttpException' object.
               throw new HttpException();
            }
         }
      
         void Button_Click(Object sender, EventArgs e)
         {
            try
            {
               CheckNumber();
               label1.Text = "The integer value you entered is: "+textbox1.Text;
            }
            catch(HttpException exp)
            {
               label1.Text = "<font color='red'>An HttpException was raised!:"
                  + " The value entered in the textbox is not an integer.</font>";
            }
         }

         void page_load(object sender,EventArgs e)
         {
            label1.Text="";
         }
      </script>
   </head>
   
   <body>
      <center>
         <h3>
            Example for HttpException
         </h3>
      </center>
      
      <form id="WebForm9" method="post" runat="server">
         <center>
         <br />
         <b>Enter a 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"></asp:Button>
         <br />
         <b><asp:Label Runat="server" ID="label1"></asp:Label></b>
         </center>
      </form>
   </body>
</html>
<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 e) {
                    // Throw a 'HttpException' object.
                    throw new HttpException();
                }
            } //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)
                {
                    label1.set_Text("<font color='red'>An HttpException was raised!:"
                        + " The value entered in the textbox is not an " 
                        + "integer.</font>");
                }
            } //Button_Click

            void page_load(Object sender,EventArgs e)
            {
                label1.set_Text("");
            } //page_load
      </script>
   </head>
   
   <body>
      <center>
         <h3>
            Example for HttpException
         </h3>
      </center>
      
      <form id="WebForm9" method="post" runat="server">
         <center>
         <br />
         <b>Enter a value in the text box.</b>
         <br />
         <asp:TextBox Runat="server" ID="textbox1"></asp:TextBox>
         <br />
         <asp:Button ID="Button1" Text="Click Here" OnClick="Button_Click" Runat="server"></asp:Button>
         <br />
         <b><asp:Label Runat="server" ID="label1"></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

その他の技術情報

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