ExceptionHandler 類別

定義

擴充 ExceptionHandler 類別,建立 Windows Communication Foundation (WCF) 執行階段中所發生未處理之例外狀況的例外處理常式。

public ref class ExceptionHandler abstract
public abstract class ExceptionHandler
type ExceptionHandler = class
Public MustInherit Class ExceptionHandler
繼承
ExceptionHandler

範例

下列程式碼範例將示範會覆寫 ExceptionHandler 方法的 HandleException 抽象類別 (Abstract Class) 實作。

using System;
using System.ServiceModel.Dispatcher;

namespace CS
{
    public class MyExceptionHandler: ExceptionHandler
    {
            // HandleException method override gives control to
            // your code.
            public override bool HandleException ( Exception ex )
            {
                // This method contains logic to decide whether
                // the exception is serious enough
                // to terminate the process.
                return ShouldTerminateProcess (ex);
            }

            public bool ShouldTerminateProcess (Exception ex)
            {
                // Write your logic here.
                return  true;
            }
    }


Imports System.ServiceModel.Dispatcher

Namespace CS
    Public Class MyExceptionHandler
        Inherits ExceptionHandler
            ' HandleException method override gives control to 
            ' your code.
            Public Overrides Function HandleException(ByVal ex As Exception) As Boolean
                ' This method contains logic to decide whether 
                ' the exception is serious enough
                ' to terminate the process.
                Return ShouldTerminateProcess (ex)
            End Function

            Public Function ShouldTerminateProcess(ByVal ex As Exception) As Boolean
                ' Write your logic here.
                Return True
            End Function
    End Class

下列程式碼範例示範如何針對 WCF 執行時間內發生的未處理例外狀況啟用自訂 MyExceptionHandler

    static void Main(string[] args)
    {
        // Create an instance of the MyExceptionHandler class.
        MyExceptionHandler thisExceptionHandler =
            new MyExceptionHandler();

        // Enable the custom handler by setting
        //   AsynchronousThreadExceptionHandler property
        //   to this object.
        ExceptionHandler.AsynchronousThreadExceptionHandler =
            thisExceptionHandler;

        // After the handler is set, write your call to
        // System.ServiceModel.ICommunication.Open here
    }
}
    Sub Main(ByVal args() As String)
        ' Create an instance of the MyExceptionHandler class.
        Dim thisExceptionHandler As New MyExceptionHandler()

        ' Enable the custom handler by setting 
        '   AsynchronousThreadExceptionHandler property
        '   to this object.
        ExceptionHandler.AsynchronousThreadExceptionHandler = thisExceptionHandler

        ' After the handler is set, write your call to 
        ' System.ServiceModel.ICommunication.Open here
    End Sub
End Module

備註

擴充 ExceptionHandler 類別並覆寫 HandleException 方法,以判定例外狀況是否應該結束應用程式。 然後建立自訂 ExceptionHandler 類別的新實例,並在建立 WCF 用戶端或服務之前,將其指派給靜態 AsynchronousThreadExceptionHandlerTransportExceptionHandler 屬性。

建構函式

ExceptionHandler()

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

屬性

AlwaysHandle

取得處理所有例外狀況的 ExceptionHandler 執行個體。

AsynchronousThreadExceptionHandler

取得或設定應用程式定義域的目前 ExceptionHandler 實作。

TransportExceptionHandler

取得或設定應用程式定義域的目前傳輸 ExceptionHandler 實作。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
HandleException(Exception)

在衍生類別中覆寫時,如果例外狀況已處理,則傳回 true,如果應該重新擲回例外狀況並結束應用程式,則傳回 false

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於