HttpException Constructors

Definition

Initializes a new instance of the HttpException class.

Overloads

HttpException()

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

HttpException(String)

Initializes a new instance of the HttpException class using a supplied error message.

HttpException(Int32, String)

Initializes a new instance of the HttpException class using an HTTP response status code and an error message.

HttpException(SerializationInfo, StreamingContext)

Initializes a new instance of the HttpException class with serialized data.

HttpException(String, Exception)

Initializes a new instance of the HttpException class using an error message and the InnerException property.

HttpException(String, Int32)

Initializes a new instance of the HttpException class using an error message and an exception code.

HttpException(Int32, String, Exception)

Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and the InnerException property.

HttpException(Int32, String, Int32)

Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and an exception code.

HttpException()

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

public HttpException ();

Examples

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.

Important

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.


<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>
            Example for HttpException
         </title>
<script language="C#" runat="server">
         void CheckNumber()
         {
            try
            {
               // Check whether the value is an integer.
               String convertInt = textbox1.Text;
               Convert.ToInt32(convertInt);
            }
            catch(Exception e)
            {
               // Throw a 'HttpException' object.
               throw new HttpException();
            }
         }
      
         void Button_Click(Object sender, EventArgs e)
         {
            try
            {
               CheckNumber();
               label1.Text = "The integer value you entered is: "+textbox1.Text;
            }
            catch(HttpException exp)
            {
               label1.Text = "<font color='red'>An HttpException was raised!:"
                  + " The value entered in the textbox is not an integer.</font>";
            }
         }

         void page_load(object sender,EventArgs e)
         {
            label1.Text="";
         }
      </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 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>

Remarks

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

HttpException(String)

Initializes a new instance of the HttpException class using a supplied error message.

public HttpException (string message);

Parameters

message
String

The error message displayed to the client when the exception is thrown.

Examples

The following code example demonstrates the HttpException constructor of the HttpException class. An HttpException is thrown if a user-entered value is 0.

Important

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.

if (Num == 0)
{
   throw new HttpException("No value entered");
}

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

HttpException(Int32, String)

Initializes a new instance of the HttpException class using an HTTP response status code and an error message.

public HttpException (int httpCode, string message);

Parameters

httpCode
Int32

The HTTP response status code sent to the client corresponding to this error.

message
String

The error message displayed to the client when the exception is thrown.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

HttpException(SerializationInfo, StreamingContext)

Initializes a new instance of the HttpException class with serialized data.

protected HttpException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);

Parameters

info
SerializationInfo

The SerializationInfo that holds the serialized object data about the exception being thrown.

context
StreamingContext

The StreamingContext that holds the contextual information about the source or destination.

Remarks

The HttpException constructor is called during deserialization to reconstitute the exception object that is transmitted over a stream. For more information, see XML and SOAP Serialization.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

HttpException(String, Exception)

Initializes a new instance of the HttpException class using an error message and the InnerException property.

public HttpException (string message, Exception innerException);

Parameters

message
String

The error message displayed to the client when the exception is thrown.

innerException
Exception

The InnerException, if any, that threw the current exception.

Examples

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 in the catch block, 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.

Important

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.


<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>Example for HttpException</title>
<script language="C#" 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 object with a message.
               throw new HttpException("THe value entered in the text box is not a integer", e);
            }
         }
      
         void Button_Click(Object sender, EventArgs e)
         {
            try
            {
               CheckNumber();
               label1.Text = "The integer value you entered is: " + textbox1.Text;
            }
            catch(HttpException exp)
            {
               // Display the exception thrown.
               label1.Text = "<font color='red'>An HttpException was raised: " + exp.Message + "</font>";
               Exception myInnerException = exp.InnerException;
               label2.Text = "InnerException is : " + myInnerException.GetType();
            }
         }

         void page_load(Object sender,EventArgs e)
         {
            label1.Text="";
            label2.Text="";
         }
      </script>
   </head>

   <body>
      <center>
         <h3>Example for HttpException</h3>
      </center>
      <form id="Form1" method="post" runat="server">
         <center>
            <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>
         </center>
      </form>
   </body>
</html>

Remarks

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

HttpException(String, Int32)

Initializes a new instance of the HttpException class using an error message and an exception code.

public HttpException (string message, int hr);

Parameters

message
String

The error message displayed to the client when the exception is thrown.

hr
Int32

The exception code that defines the error.

Examples

The following code example demonstrates the HttpException constructor of the HttpException class. An HttpException exception is thrown if a user-entered value is 0.

Important

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.

if (Num == 0)
{
   throw new HttpException("No value entered", 100);
}

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

HttpException(Int32, String, Exception)

Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and the InnerException property.

public HttpException (int httpCode, string message, Exception innerException);

Parameters

httpCode
Int32

The HTTP response status code displayed on the client.

message
String

The error message displayed to the client when the exception is thrown.

innerException
Exception

The InnerException, if any, that threw the current exception.

Examples

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.

Important

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" %>
<!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>Example for HttpException</title>
<script language="C#" runat="server">
         void CheckNumber()
         {
            try
            {
               // Check whether the value is an integer.
               String convertInt = textbox1.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);
            }
         }
         
         void Button_Click(Object sender, EventArgs e)
         {
            try
            {
               CheckNumber();
               label1.Text = "The integer value you entered is: " + textbox1.Text;
            }
            catch(HttpException exp)
            {
               // Display the exception thrown.
               label1.ForeColor = Color.Red;
               label1.Text = "An HttpException was raised!: " + exp.Message;
               Exception myInnerException = exp.InnerException;
               
               // Display the inner exception.
               label2.Text = "The InnerException is : " + myInnerException.GetType();
                
            }
         }
     
         void page_load(Object sender,EventArgs e)
         {
           label1.Text="";
           label2.Text="";
         }

      </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>
            <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>

Remarks

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

HttpException(Int32, String, Int32)

Initializes a new instance of the HttpException class using an HTTP response status code, an error message, and an exception code.

public HttpException (int httpCode, string message, int hr);

Parameters

httpCode
Int32

The HTTP response status code displayed on the client.

message
String

The error message displayed to the client when the exception is thrown.

hr
Int32

The exception code that defines the error.

Examples

The following code example demonstrates the HttpException constructor of the HttpException class. The user name and email 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.

Important

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.


<!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="C#" runat="server">
         void SubmitButton_Click(Object sender, EventArgs e)
         {
            try
            {
               if(Textbox1.Text.Length==0 || Textbox2.Text.Length==0)
               {
                  // Raise an Exception if the username or the emailfield field is empty.
                  throw new HttpException(901,"User name or email ID not provided.",333);
               }
               else
               {
                  MyLabel.Text="Hello "+Textbox1.Text+"<br />";
                  MyLabel.Text+="The Weekly newsletter is mailed to :"+
                           Textbox2.Text+"<br />";
               }
            }
            catch(HttpException ex)
            { 
               // Display the error code returned by the GetHttpCode method.
               MyLabel.Text="<h4><font color=\"red\">The exception is "+
                  ex.GetHttpCode() +" - "+ ex.Message + "</font></h4>";
            }
         }

         void Page_Load(object sender,EventArgs e)
         {
            MyLabel.Text="";
         }
      </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 />
         Email 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>

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1