Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ExceptionHandler Class

Extend the ExceptionHandler class to create an exception handler for unhandled exceptions that occur within the runtime.

System.Object
  System.ServiceModel.Dispatcher.ExceptionHandler

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

'Declaration
Public MustInherit Class ExceptionHandler

The ExceptionHandler type exposes the following members.

  NameDescription
Protected methodExceptionHandlerInitializes a new instance of the ExceptionHandler class.
Top

  NameDescription
Public propertyStatic memberAlwaysHandleGets an instance of ExceptionHandler that handles all exceptions.
Public propertyStatic memberAsynchronousThreadExceptionHandlerGets or sets the current ExceptionHandler implementation for the application domain.
Public propertyStatic memberTransportExceptionHandlerGets or sets the current transport ExceptionHandler implementation for the application domain.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodHandleExceptionWhen overridden in a derived class, returns true if the exception has been handled, or false if the exception should be rethrown and the application terminated.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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 clients or services.

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




Imports System
Imports System.ServiceModel.Dispatcher ' ExceptionHandler

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



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


    Sub Main(ByVal args() As String)
        ' Create an instance of the MyExceptionHander 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


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

Community Additions

Show:
© 2017 Microsoft