HttpException クラス
アセンブリ: System.Web (system.web.dll 内)


HttpException クラスは HTTP 固有の例外クラスで、ASP.NET はこれを使用して例外情報を生成できます。例外のスローと処理の詳細については、「例外の処理とスロー」を参照してください。

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>


System.Exception
System.SystemException
System.Runtime.InteropServices.ExternalException
System.Web.HttpException
System.Web.HttpCompileException
System.Web.HttpParseException
System.Web.HttpRequestValidationException
System.Web.HttpUnhandledException


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- HttpException クラスのページへのリンク