HttpException Constructor (Int32, String, Exception)
Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and the InnerException property.
Assembly: System.Web (in System.Web.dll)
Parameters
- httpCode
-
Type:
System.Int32
The HTTP response status code displayed on the client.
- message
-
Type:
System.String
The error message displayed to the client when the exception is thrown.
- innerException
-
Type:
System.Exception
The InnerException, if any, that threw the current exception.
When handling exceptions, it can be useful to capture a series of related exceptions with the outer exception being thrown in response to an inner exception.
A reference to the inner exception that caused the outer exception is available from the InnerException property of the outer exception. This mechanism preserves the error information that is carried by earlier exceptions, including the original exceptions, while allowing you to create more meaningful outer exceptions. For more information, see InnerException.
The following code example demonstrates the HttpException constructor of the HttpException class. The CheckNumber method accepts a user-entered value and checks whether it is an integer. If the value is not an integer, an exception is thrown, and then a new HttpException object containing the HTTP response status code, the exception's message, and any inner exception is created. That exception is caught in the Button_Click event handler and the error message, error code, and inner exception are displayed.
Security Note
|
|---|
This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview. |
<%@ Import Namespace="System.Drawing" %> <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 integer. Dim convertInt As [String] = textbox1.Text Convert.ToInt32(convertInt) Catch ex As Exception ' Throw an HttpException object that contains the HTTP error code, ' message, and inner exception. Throw New HttpException(500, "The entered value is not an integer.", ex) End Try End Sub 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 ' Display the Exception thrown. label1.ForeColor = Color.Red label1.Text = "An HttpException was raised: " & exp.Message Dim myInnerException As Exception = exp.InnerException ' Display the inner exception. label2.Text = "InnerException is : " & myInnerException.GetType().ToString() End Try End Sub Sub page_load(sender As [Object], e As EventArgs) label1.Text = "" label2.Text = "" End Sub </script> </head> <body> <center> <h3>Example for HttpException</h3> <form id="WebForm9" method="post" runat="server"> <b>Enter the 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" ID="Button1"></asp:Button> <br /> <b> <asp:Label Runat="server" ID="label1"></asp:Label> <br /> <asp:Label Runat="server" ID="label2"></asp:Label> </b> </form> </center> </body> </html>
Available since 1.1
