HttpException 构造函数 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 HttpException 类的新实例。
重载
| HttpException() | 初始化 HttpException 类的新实例并创建一个空 HttpException 对象。 | 
| HttpException(String) | 使用所提供的错误消息初始化 HttpException 类的新实例。 | 
| HttpException(Int32, String) | 使用 HTTP 响应状态代码和错误消息初始化 HttpException 类的新实例。 | 
| HttpException(SerializationInfo, StreamingContext) | 用序列化数据初始化 HttpException 类的新实例。 | 
| HttpException(String, Exception) | 使用错误消息和 HttpException 属性初始化 InnerException 类的新实例。 | 
| HttpException(String, Int32) | 使用错误消息和异常代码初始化 HttpException 类的新实例。 | 
| HttpException(Int32, String, Exception) | 使用 HTTP 响应状态代码、错误消息以及 HttpException 属性初始化 InnerException 类的新实例。 | 
| HttpException(Int32, String, Int32) | 使用 HTTP 响应状态代码、错误消息和异常代码初始化 HttpException 类的新实例。 | 
HttpException()
初始化 HttpException 类的新实例并创建一个空 HttpException 对象。
public:
 HttpException();public HttpException ();Public Sub New ()示例
下面的代码示例演示 HttpException 类的 HttpException 构造函数。 方法 CheckNumber 通过文本框接受用户输入的值,并检查它是否为整数。 如果值不是整数,则会引发异常,然后创建并引发一个新的 HttpException 对象。 该异常在事件处理程序中 Button_Click 捕获,并在浏览器中显示错误消息。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<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>
<!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="VB" runat="server">
         Sub CheckNumber()
            Try
               ' Check whether the value is an integer.
               Dim convertInt As [String] = textbox1.Text
               Convert.ToInt32(convertInt)
            Catch e As Exception
               ' Throw the 'HttpException' object.
               Throw New HttpException()
            End Try
         End Sub 'CheckNumber
 
         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
               label1.Text = "<font color='red'>An HttpException was raised!:" _
                  & " The value entered in the textbox is not an integer</font>"
            End Try
         End Sub 'Button_Click
       
         Sub Page_Load(sender As [Object], e As EventArgs)
            label1.Text=""
         End Sub
      </script>
   </head>
   <body>
      <center>
         <h3>Example for HttpException</h3>
      </center>
      <form id="WebForm9" method="post" runat="server">
         <center>
            <b>Enter a 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"></asp:Button>
            <br />
            <b><asp:Label Runat="server" ID="label1"></asp:Label></b>
         </center>
      </form>
   </body>
</html>
注解
处理异常时,捕获一系列相关的异常(其中引发外部异常以响应内部异常)可能很有用。
外部 InnerException 异常的 属性中提供了对导致外部异常的内部异常的引用。 此机制保留早期异常(包括原始异常)携带的错误信息,同时允许创建更有意义的外部异常。 有关详细信息,请参阅 InnerException。
另请参阅
适用于
HttpException(String)
使用所提供的错误消息初始化 HttpException 类的新实例。
public:
 HttpException(System::String ^ message);public HttpException (string message);new System.Web.HttpException : string -> System.Web.HttpExceptionPublic Sub New (message As String)参数
- message
- String
当发生异常时向客户端显示的错误消息。
示例
下面的代码示例演示 HttpException 类的 HttpException 构造函数。 HttpException如果用户输入的值为 0,则会引发 。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
if (Num == 0)
{
   throw new HttpException("No value entered");
}
If Num = 0 Then
   Throw New HttpException("No value entered")
end if
另请参阅
适用于
HttpException(Int32, String)
使用 HTTP 响应状态代码和错误消息初始化 HttpException 类的新实例。
public:
 HttpException(int httpCode, System::String ^ message);public HttpException (int httpCode, string message);new System.Web.HttpException : int * string -> System.Web.HttpExceptionPublic Sub New (httpCode As Integer, message As String)参数
- httpCode
- Int32
发送到对应于此错误的客户端的 HTTP 响应状态代码。
- message
- String
当发生异常时向客户端显示的错误消息。
另请参阅
适用于
HttpException(SerializationInfo, StreamingContext)
用序列化数据初始化 HttpException 类的新实例。
protected:
 HttpException(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);protected HttpException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);new System.Web.HttpException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Web.HttpExceptionProtected Sub New (info As SerializationInfo, context As StreamingContext)参数
- info
- SerializationInfo
包含有关所引发异常的序列化对象数据的 SerializationInfo。
- context
- StreamingContext
StreamingContext,它包含有关源或目标的上下文信息。
注解
构造 HttpException 函数在反序列化期间调用,以重建通过流传输的异常对象。 有关详细信息,请参阅 XML 和 SOAP 序列化。
另请参阅
适用于
HttpException(String, Exception)
使用错误消息和 HttpException 属性初始化 InnerException 类的新实例。
public:
 HttpException(System::String ^ message, Exception ^ innerException);public HttpException (string message, Exception innerException);new System.Web.HttpException : string * Exception -> System.Web.HttpExceptionPublic Sub New (message As String, innerException As Exception)参数
- message
- String
当发生异常时向客户端显示的错误消息。
- innerException
- Exception
引发了当前异常的 InnerException(如果有)。
示例
下面的代码示例演示 HttpException 类的 HttpException 构造函数。 方法 CheckNumber 通过文本框接受用户输入的值,并检查它是否为整数。 如果值不是整数,则会引发异常,然后在 catch 块中创建并引发一个新的 HttpException 对象。 该异常在事件处理程序中 Button_Click 捕获,并在浏览器中显示错误消息。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<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>
<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 an integer.
               Dim convertInt As [String] = textbox1.Text
               Convert.ToInt32(convertInt)
            Catch e As Exception
               ' Throw an HttpException object with a message.
               Throw New HttpException("The value entered in the textbox is not a integer", e)
            End Try
         End Sub 'CheckNumber
       
         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.Text = "<font color='red'>An HttpException was raised!: " + exp.Message + "</font>"
               Dim myInnerException As Exception = exp.InnerException
               label2.Text = "InnerException is : " + myInnerException.GetType().ToString()
            End Try
         End Sub 'Button_Click
       
         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>
注解
处理异常时,捕获一系列相关的异常(其中引发外部异常以响应内部异常)可能很有用。
外部 InnerException 异常的 属性中提供了对导致外部异常的内部异常的引用。 此机制保留早期异常(包括原始异常)携带的错误信息,同时允许创建更有意义的外部异常。 有关详细信息,请参阅 InnerException。
另请参阅
适用于
HttpException(String, Int32)
使用错误消息和异常代码初始化 HttpException 类的新实例。
public:
 HttpException(System::String ^ message, int hr);public HttpException (string message, int hr);new System.Web.HttpException : string * int -> System.Web.HttpExceptionPublic Sub New (message As String, hr As Integer)参数
- message
- String
当发生异常时向客户端显示的错误消息。
- hr
- Int32
定义错误的异常代码。
示例
下面的代码示例演示 HttpException 类的 HttpException 构造函数。 HttpException如果用户输入的值为 0,则会引发异常。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
if (Num == 0)
{
   throw new HttpException("No value entered", 100);
}
If Num = 0 Then
   Throw New HttpException("No value entered", 100)
end if
另请参阅
适用于
HttpException(Int32, String, Exception)
使用 HTTP 响应状态代码、错误消息以及 HttpException 属性初始化 InnerException 类的新实例。
public:
 HttpException(int httpCode, System::String ^ message, Exception ^ innerException);public HttpException (int httpCode, string message, Exception innerException);new System.Web.HttpException : int * string * Exception -> System.Web.HttpExceptionPublic Sub New (httpCode As Integer, message As String, innerException As Exception)参数
- httpCode
- Int32
在客户端上显示的 HTTP 响应状态代码。
- message
- String
当发生异常时向客户端显示的错误消息。
- innerException
- Exception
引发了当前异常的 InnerException(如果有)。
示例
下面的代码示例演示 HttpException 类的 HttpException 构造函数。 方法 CheckNumber 接受用户输入的值,并检查它是否为整数。 如果值不是整数,则会引发异常,然后创建包含 HTTP 响应状态代码、异常消息和任何内部异常的新 HttpException 对象。 该异常在事件处理程序中 Button_Click 捕获,并显示错误消息、错误代码和内部异常。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ 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>
<%@ 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>
注解
处理异常时,捕获一系列相关的异常(其中引发外部异常以响应内部异常)可能很有用。
外部 InnerException 异常的 属性中提供了对导致外部异常的内部异常的引用。 此机制保留早期异常(包括原始异常)携带的错误信息,同时允许创建更有意义的外部异常。 有关详细信息,请参阅 InnerException。
另请参阅
适用于
HttpException(Int32, String, Int32)
使用 HTTP 响应状态代码、错误消息和异常代码初始化 HttpException 类的新实例。
public:
 HttpException(int httpCode, System::String ^ message, int hr);public HttpException (int httpCode, string message, int hr);new System.Web.HttpException : int * string * int -> System.Web.HttpExceptionPublic Sub New (httpCode As Integer, message As String, hr As Integer)参数
- httpCode
- Int32
在客户端上显示的 HTTP 响应状态代码。
- message
- String
当发生异常时向客户端显示的错误消息。
- hr
- Int32
定义错误的异常代码。
示例
下面的代码示例演示 HttpException 类的 HttpException 构造函数。 用户名和电子邮件信息由用户在提供的文本框中输入。 如果任何文本框留空, HttpException 则会创建并引发 对象。 的 HttpException 错误代码由 GetHttpCode 方法获取,并显示在网页上。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<!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>
<!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="VB" runat="server">
         Sub SubmitButton_Click(sender As Object, e As EventArgs)
            Try
               If Textbox1.Text.Length = 0 Or Textbox2.Text.Length = 0 Then
                  ' Raise an Exception if the username or emailid 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 />"
               End If
            Catch ex As HttpException
               ' Display the error code returned by the GetHttpCode method.
            MyLabel.Text = "<h4><font color=""red"">The exception is " & ex.GetHttpCode() & _
               " - " & ex.Message & "</font></h4>"
            End Try
         End Sub
         Sub Page_Load(sender As Object, e As EventArgs)
            MyLabel.Text = ""
         End Sub
      </script>
   </head>
   <body>
      <form runat="server" id="Form1">
         <h3>HttpException Example</h3>
         Enter User name and Email
         <br /><br />
         User Name:
         <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>