HttpException クラスとは? わかりやすく解説

HttpException クラス

HTTP 要求の処理中に発生した例外説明します

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

<SerializableAttribute> _
Public Class HttpException
    Inherits ExternalException
Dim instance As HttpException
[SerializableAttribute] 
public class HttpException : ExternalException
[SerializableAttribute] 
public ref class HttpException : public
 ExternalException
/** @attribute SerializableAttribute() */ 
public class HttpException extends ExternalException
SerializableAttribute 
public class HttpException extends
 ExternalException
解説解説
使用例使用例

HttpException クラス使用してカスタマイズされた例外発生させる方法次のコード例示しますButton_Click メソッドは、クリックされたボタン判断しCheckNumber メソッドまたは CheckBoolean メソッド呼び出します。ユーザーテキスト ボックス入力した値が必要な型と一致しない場合は、HttpException 例外作成されエラーの型に基づいてカスタマイズされたメッセージと共にスローさます。

<%@ Page language="VB" %>

<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>
   <head>
     <title>HttpException Example</title>
   </head>
   <body>
     <h3>HttpException Example</h3>
      
     <form id="Form1" runat="server">
        <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>
     </form>
   </body>
</html>
<%@ Page language="C#" %>

<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>
   <head>
     <title>HttpException Example</title>
   </head>
   <body>
     <h3>HttpException Example</h3>
      
     <form runat="server">
        <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>
     </form>
   </body>
</html>
.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.Exception
     System.SystemException
       System.Runtime.InteropServices.ExternalException
        System.Web.HttpException
           System.Web.HttpCompileException
           System.Web.HttpParseException
           System.Web.HttpRequestValidationException
           System.Web.HttpUnhandledException
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「HttpException クラス」の関連用語

HttpException クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



HttpException クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS