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 抽象类的实现。

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 方法可以确定异常是否应终止应用程序。 然后,在创建 WCF 客户端或服务之前,创建自定义 ExceptionHandler 类的新实例并将其分配给静态 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)

适用于