HttpException Constructor (Int32, String, Exception)
Assembly: System.Web (in system.web.dll)
public HttpException ( int httpCode, String message, Exception innerException )
public function HttpException ( httpCode : int, message : String, innerException : Exception )
Parameters
- httpCode
The HTTP response status code displayed on the client.
- message
The error message displayed to the client when the exception is thrown.
- innerException
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.
<%@ Import Namespace="System.Drawing" %>
<html>
<head>
<script language="VJ#" runat="server">
void CheckNumber() throws HttpException
{
try {
// Check whether the value is an integer.
String convertInt = textbox1.get_Text();
Convert.ToInt32(convertInt);
}
catch(Exception ex) {
// 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);
}
} //CheckNumber
void Button_Click(Object sender, EventArgs e)
{
try {
CheckNumber();
label1.set_Text("The integer value you entered is: "
+ textbox1.get_Text());
}
catch(HttpException exp) {
// Display the exception thrown.
label1.set_ForeColor(Color.get_Red());
label1.set_Text("An HttpException was raised!: "
+ exp.get_Message());
System.Exception myInnerException = exp.get_InnerException();
// Display the inner exception.
label2.set_Text("The InnerException is : "
+ myInnerException.GetType());
}
} //Button_Click
void page_load(Object sender,EventArgs e)
{
label1.set_Text("");
label2.set_Text("");
} //page_load
</script>
</head>
<body MS_POSITIONING="GridLayout">
<center>
<h3>Example for HttpException</h3>
<form id="WebForm9" method="post" runat="server">
<b>Enter the value in the text box </b>
<br>
<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>
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.