This documentation is archived and is not being maintained.

ExceptionHandler Class

Extend the ExceptionHandler class to create an exception handler for unhandled exceptions that occur within the Windows Communication Foundation (WCF) runtime.

Namespace: System.ServiceModel.Dispatcher
Assembly: System.ServiceModel (in system.servicemodel.dll)

public abstract class ExceptionHandler
public abstract class ExceptionHandler
public abstract class ExceptionHandler
Not applicable.

Extend the ExceptionHandler class and override the HandleException method to determine whether an exception should terminate the application. Then create a new instance of your custom ExceptionHandler class and assign it to the static AsynchronousThreadExceptionHandler or TransportExceptionHandler property prior to creating WCF clients or services.

The following code example shows an implementation of the ExceptionHandler abstract class that overrides the HandleException method.

using System;
using System.ServiceModel.Dispatcher; // ExceptionHandler

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;

The following code example shows how to enable the custom MyExceptionHandler for unhandled exceptions that occur within the WCF runtime.

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

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

        // After the handler is set, write your call to 
        // System.ServiceModel.ICommunation.Open here


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0