ExceptionHandler.HandleException(Exception) Méthode

Définition

En cas de substitution dans une classe dérivée, retourne true si l’exception a été gérée, ou false si l’exception doit être rethrown et que l’application s’est arrêtée.

public:
 abstract bool HandleException(Exception ^ exception);
public abstract bool HandleException(Exception exception);
abstract member HandleException : Exception -> bool
Public MustOverride Function HandleException (exception As Exception) As Boolean

Paramètres

exception
Exception

Exception qui s’est produite dans le runtime Windows Communication Foundation (WCF) et qui peut mettre fin à l’application.

Retours

true si l’exception a été gérée ; sinon, false.

Exemples

L’exemple de code suivant montre une implémentation de la ExceptionHandler classe abstraite qui remplace la HandleException méthode.

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

L’exemple de code suivant montre comment activer la fonctionnalité personnalisée MyExceptionHandler pour les exceptions non gérées qui se produisent dans le runtime WCF.

// 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

Remarques

La HandleException(Exception) méthode retourne true si l’exception a été gérée. Si elle retourne false ou lève une autre exception, l’exception d’origine est rethrown.

S’applique à