WorkflowApplicationUnhandledExceptionEventArgs.UnhandledException 屬性

定義

取得工作流程執行個體未處理的 Exception

public:
 property Exception ^ UnhandledException { Exception ^ get(); };
public Exception UnhandledException { get; }
member this.UnhandledException : Exception
Public ReadOnly Property UnhandledException As Exception

屬性值

工作流程執行個體未處理的 Exception

範例

下列範例會叫用擲回例外狀況的工作流程。 此例外狀況未由工作流程處理,而且叫用了 OnUnhandledException 處理常式。 系統會檢查 WorkflowApplicationUnhandledExceptionEventArgs 以提供例外狀況的相關資訊,並且終止工作流程。

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();

備註

如果活動擲回例外狀況,且該例外狀況未經處理,預設的行為是終止該工作流程執行個體。 如果存在 OnUnhandledException 處理常式,它可能會覆寫這個預設行為。 這個處理常式可讓工作流程主機作者提供適當的處理,例如自訂登入、中止工作流程、取消工作流程,或者終止工作流程。

適用於