HttpException Class
Assembly: System.Web (in system.web.dll)
The HttpException class is an HTTP-specific exception class that enables ASP.NET to generate exception information. For more information about throwing and handling exceptions, see Handling and Throwing Exceptions.
The following code example demonstrates how to use the HttpException class to raise customized exceptions. The Button_Click method determines which button was clicked, and then calls either the CheckNumber or CheckBoolean method. If the user-entered value in the text box does not correspond to the expected type, an HttpException exception is created and thrown with a customized message based on the error type.
<%@ 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>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.