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="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>
- 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.