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如果存在处理程序,它可以替代此默认行为。 此处理程序使工作流主机作者有机会提供适当的处理,例如自定义日志记录、中止工作流、取消工作流或终止工作流。

适用于