HttpException.HttpException() Constructor

Initializes a new instance of the HttpException class and creates an empty HttpException object.

Namespace: System.Web
Assembly: System.Web (in system.web.dll)

public:
HttpException ()
public HttpException ()
public function HttpException ()
Not applicable.

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 through a text box and checks whether it is an integer. If the value is not an integer, an exception is thrown, and then a new HttpException object is created and thrown. That exception is caught in the Button_Click event handler and the error message is displayed on the browser.

Security noteSecurity 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).

No code example is currently available or this language may not be supported.

<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>
            Example for HttpException
         </title>
<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 e) {
                    // Throw a 'HttpException' object.
                    throw new HttpException();
                }
            } //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)
                {
                    label1.set_Text("<font color='red'>An HttpException was raised!:"
                        + " The value entered in the textbox is not an " 
                        + "integer.</font>");
                }
            } //Button_Click

            void page_load(Object sender,EventArgs e)
            {
                label1.set_Text("");
            } //page_load
      </script>
   </head>
   
   <body>
      <center>
         <h3>
            Example for HttpException
         </h3>
      </center>
      
      <form id="WebForm9" method="post" runat="server">
         <center>
         <br />
         <b>Enter a value in the text box.</b>
         <br />
         <asp:TextBox Runat="server" ID="textbox1"></asp:TextBox>
         <br />
         <asp:Button ID="Button1" Text="Click Here" OnClick="Button_Click" Runat="server"></asp:Button>
         <br />
         <b><asp:Label Runat="server" ID="label1"></asp:Label></b>
         </center>
      </form>
   </body>
</html>

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: