ExceptionHandler.HandleException(Exception) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
När det åsidosättas i en härledd klass returneras true om undantaget har hanterats, eller false om undantaget ska förnyas och programmet avslutas.
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
Parametrar
- exception
- Exception
Undantaget som inträffade inom Windows Communication Foundation(WCF) körning och som kan avsluta programmet.
Returer
trueom undantaget har hanterats. annars . false
Exempel
I följande kodexempel visas en implementering av den ExceptionHandler abstrakta klass som åsidosätter HandleException metoden.
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
I följande kodexempel visas hur du aktiverar anpassad MyExceptionHandler för ohanterade undantag som inträffar inom WCF-körningen.
// 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
Kommentarer
Metoden HandleException(Exception) returnerar true om undantaget har hanterats. Om det returnerar false eller utlöser ett annat undantag, är det ursprungliga undantaget återväxt.