HttpException.HttpException(Int32, String, Int32) Constructor
Assembly: System.Web (in system.web.dll)
public HttpException ( int httpCode, String message, int hr )
public function HttpException ( httpCode : int, message : String, hr : int )
Not applicable.
Parameters
- httpCode
The HTTP response status code displayed on the client.
- message
The error message displayed to the client when the exception is thrown.
- hr
The exception code that defines the error.
The following code example demonstrates the HttpException constructor of the HttpException class. The user name and e-mail information are entered by the user in the provided text boxes. If any of the text boxes are left blank, an HttpException object is created and thrown. The error code of the HttpException is obtained by the GetHttpCode method and displayed on the Web page.
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 (Visual Studio). |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HttpException Example</title>
<script language="VJ#" runat="server">
void SubmitButton_Click(Object sender, EventArgs e)
{
try {
if(Textbox1.get_Text().get_Length()==0 ||
Textbox2.get_Text().get_Length()==0) {
// Raise an Exception if the username or the emailfield
// field is empty.
throw new HttpException(901,"User name or e-mail "
+ "ID not provided.",333);
}
else {
MyLabel.set_Text("Hello "+Textbox1.get_Text()+"<br />");
MyLabel.set_Text(MyLabel.get_Text()
+ "The Weekly newsletter is mailed to :"
+ Textbox2.get_Text()+"<br />");
}
}
catch(HttpException ex) {
// Display the error code returned by the GetHttpCode method.
MyLabel.set_Text("<h4><font color=\"red\">The exception is "
+ ex.GetHttpCode() +" - "+ ex.get_Message()
+ "</font></h4>");
}
} //SubmitButton_Click
void Page_Load(Object sender,EventArgs e)
{
MyLabel.set_Text("");
} //Page_Load
</script>
</head>
<body>
<form runat="server" id="Form1">
<h3>HttpException Example</h3>
Enter UserName and Email
<br /><br />
UserName :
<asp:TextBox ID="Textbox1" Runat="server"></asp:TextBox>
<br />
E-mail ID :
<asp:TextBox ID="Textbox2" Runat="server"></asp:TextBox>
<asp:Button ID="Button1" Text="Submit" OnClick="SubmitButton_Click" runat="server" />
<br />
<asp:label id="MyLabel" runat="server" />
</form>
</body>
</html>
Security Note: