WorkflowApplication.OnUnhandledException 屬性

定義

取得或設定現有工作流程執行個體遭遇未處理例外狀況時,會叫用的 Func<T,TResult>Gets or sets the Func<T,TResult> that is invoked when the current workflow instance encounters an unhandled exception.

public:
 property Func<System::Activities::WorkflowApplicationUnhandledExceptionEventArgs ^, System::Activities::UnhandledExceptionAction> ^ OnUnhandledException { Func<System::Activities::WorkflowApplicationUnhandledExceptionEventArgs ^, System::Activities::UnhandledExceptionAction> ^ get(); void set(Func<System::Activities::WorkflowApplicationUnhandledExceptionEventArgs ^, System::Activities::UnhandledExceptionAction> ^ value); };
public Func<System.Activities.WorkflowApplicationUnhandledExceptionEventArgs,System.Activities.UnhandledExceptionAction> OnUnhandledException { get; set; }
member this.OnUnhandledException : Func<System.Activities.WorkflowApplicationUnhandledExceptionEventArgs, System.Activities.UnhandledExceptionAction> with get, set
Public Property OnUnhandledException As Func(Of WorkflowApplicationUnhandledExceptionEventArgs, UnhandledExceptionAction)

屬性值

工作流程執行個體碰上未處理例外狀況時,會叫用的委派。The delegate that is invoked when a workflow instance encounters an unhandled exception.

範例

下列範例會叫用擲回例外狀況的工作流程。The following example invokes a workflow that throws an exception. 此例外狀況未由工作流程處理,而且叫用了 OnUnhandledException 處理常式。The exception is unhandled by the workflow and the OnUnhandledException handler is invoked. 系統會檢查 WorkflowApplicationUnhandledExceptionEventArgs 以提供例外狀況的相關資訊,並且終止工作流程。The WorkflowApplicationUnhandledExceptionEventArgs are inspected to provide information about the exception, and the workflow is terminated.

Activity wf = new Sequence
{
    Activities =
     {
         new WriteLine
         {
             Text = "Starting the workflow."
         },
         new Throw
        {
            Exception = new InArgument<Exception>((env) => 
                new ApplicationException("Something unexpected happened."))
        },
        new WriteLine
         {
             Text = "Ending the workflow."
         }
     }
};

WorkflowApplication wfApp = new WorkflowApplication(wf);

wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e)
{
    // Display the unhandled exception.
    Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}",
        e.InstanceId, e.UnhandledException.Message);

    Console.WriteLine("ExceptionSource: {0} - {1}",
        e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId);

    // Instruct the runtime to terminate the workflow.
    return UnhandledExceptionAction.Terminate;

    // Other choices are UnhandledExceptionAction.Abort and 
    // UnhandledExceptionAction.Cancel
};

wfApp.Run();

備註

當工作流程中有未處理的例外狀況時,OnUnhandledExceptionWorkflowUnhandledExceptionBehavior 都會指定執行階段的行為,不過,WorkflowUnhandledExceptionBehavior 可以選擇離開持續性存放區中暫止的工作流程,而 OnUnhandledException 不能。Both OnUnhandledException and WorkflowUnhandledExceptionBehavior dictate the behavior of the runtime when an exception is not handled in the workflow; however, WorkflowUnhandledExceptionBehavior has the option of leaving a suspended workflow in the persistence store, while OnUnhandledException does not. 這是因為暫止的工作流程所發生的情形是主機特定的情形,而 WorkflowApplication 不是。The reason for this is that what happens to a suspended workflow is host-specific, and WorkflowApplication is not. 若要使用 WorkflowApplication 來實作這個功能,請建立具有這個行為的自訂 PersistenceParticipantTo implement this functionality using WorkflowApplication, create a custom PersistenceParticipant that has this behavior.

適用於