ExceptionHandler.HandleException(Exception) Método

Definição

Quando sobrescrito numa classe derivada, retorna true se a exceção já foi tratada, ou false se a exceção deve ser relançada e a aplicação terminada.

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

Parâmetros

exception
Exception

A exceção que ocorria dentro do runtime do Windows Communication Foundation (WCF) e que podia terminar a aplicação.

Devoluções

true se a exceção já foi tratada; caso contrário, false.

Exemplos

O exemplo de código seguinte mostra uma implementação da ExceptionHandler classe abstrata que sobrepõe o HandleException método.

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

O seguinte exemplo de código mostra como ativar o costume MyExceptionHandler para exceções não tratadas que ocorrem dentro do 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

Observações

O HandleException(Exception) método retorna true se a exceção já foi tratada. Se devolver false ou lançar uma exceção diferente, a exceção original é relançada.

Aplica-se a