共用方式為


HttpException 建構函式

定義

初始化 HttpException 類別的新執行個體。

多載

名稱 Description
HttpException()

初始化該類別的新實例 HttpException 並建立一個空 HttpException 物件。

HttpException(String)

利用提供的錯誤訊息初始化該類別的新 HttpException 實例。

HttpException(Int32, String)

使用 HTTP 回應狀態碼及錯誤訊息初始化該 HttpException 類別的新實例。

HttpException(SerializationInfo, StreamingContext)

初始化一個新的類別實例 HttpException ,並使用序列化資料。

HttpException(String, Exception)

利用錯誤訊息和屬性InnerException初始化該HttpException類別的新實例。

HttpException(String, Int32)

使用錯誤訊息和例外碼初始化該類別的新 HttpException 實例。

HttpException(Int32, String, Exception)

使用 HTTP 回應狀態碼、錯誤訊息及InnerException屬性初始化該HttpException類別的新實例。

HttpException(Int32, String, Int32)

使用HTTP回應狀態碼、錯誤訊息及例外碼初始化該類別的新實例 HttpException

HttpException()

初始化該類別的新實例 HttpException 並建立一個空 HttpException 物件。

public:
 HttpException();
public HttpException();
Public Sub New ()

範例

以下程式碼範例展示了 HttpExceptionHttpException 類別的建構子。 此 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.HttpException
Public Sub New (message As String)

參數

message
String

當異常被拋出時,錯誤訊息會顯示給客戶端。

範例

以下程式碼範例展示了 HttpExceptionHttpException 類別的建構子。 若使用者輸入的值為 0,則會拋出 An HttpException

這很重要

此範例中有一個文字框可接受使用者輸入,這可能構成安全威脅。 預設情況下,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.HttpException
Public 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.HttpException
Protected Sub New (info As SerializationInfo, context As StreamingContext)

參數

info
SerializationInfo

SerializationInfo 個 保留了關於被拋出例外的序列化物件資料。

context
StreamingContext

那個 StreamingContext 包含了關於來源或目的地的上下文資訊。

備註

在反序列化過程中會呼叫建 HttpException 構子,以重建透過串流傳輸的例外物件。 欲了解更多資訊,請參閱 XML 與 SOAP 序列化

另請參閱

適用於

HttpException(String, Exception)

利用錯誤訊息和屬性InnerException初始化該HttpException類別的新實例。

public:
 HttpException(System::String ^ message, Exception ^ innerException);
public HttpException(string message, Exception innerException);
new System.Web.HttpException : string * Exception -> System.Web.HttpException
Public Sub New (message As String, innerException As Exception)

參數

message
String

當異常被拋出時,錯誤訊息會顯示給客戶端。

innerException
Exception

InnerException如果有的話,那就是目前的例外。

範例

以下程式碼範例展示了 HttpExceptionHttpException 類別的建構子。 此 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 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.HttpException
Public Sub New (message As String, hr As Integer)

參數

message
String

當異常被拋出時,錯誤訊息會顯示給客戶端。

hr
Int32

定義錯誤的例外程式碼。

範例

以下程式碼範例展示了 HttpExceptionHttpException 類別的建構子。 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 回應狀態碼、錯誤訊息及InnerException屬性初始化該HttpException類別的新實例。

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.HttpException
Public Sub New (httpCode As Integer, message As String, innerException As Exception)

參數

httpCode
Int32

HTTP 回應狀態碼顯示在用戶端。

message
String

當異常被拋出時,錯誤訊息會顯示給客戶端。

innerException
Exception

InnerException如果有的話,那就是目前的例外。

範例

以下程式碼範例展示了 HttpExceptionHttpException 類別的建構子。 此 CheckNumber 方法接受使用者輸入的值,並檢查是否為整數。 若值非整數,會拋出例外,接著建立包含 HttpException HTTP 回應狀態碼、例外訊息及任何內部例外的新物件。 該例外會被 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.HttpException
Public Sub New (httpCode As Integer, message As String, hr As Integer)

參數

httpCode
Int32

HTTP 回應狀態碼顯示在用戶端。

message
String

當異常被拋出時,錯誤訊息會顯示給客戶端。

hr
Int32

定義錯誤的例外程式碼。

範例

以下程式碼範例展示了 HttpExceptionHttpException 類別的建構子。 使用者會在提供的文字框中輸入使用者名稱和電子郵件資訊。 如果有任何文字框留空,則 HttpException 會建立並拋出一個物件。 該方法會取得GetHttpCode錯誤代碼HttpException並顯示在網頁上。

這很重要

此範例中有一個文字框可接受使用者輸入,這可能構成安全威脅。 預設情況下,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>

另請參閱

適用於