Rediger

Del via


WorkflowApplicationUnhandledExceptionEventArgs.UnhandledException Property

Definition

Gets the Exception that was unhandled by the workflow instance.

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

Property Value

The Exception that was unhandled by the workflow instance.

Examples

The following example invokes a workflow that throws an exception. The exception is unhandled by the workflow and the OnUnhandledException handler is invoked. 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();

Remarks

If an exception is thrown by an activity and is unhandled, the default behavior is to terminate the workflow instance. If an OnUnhandledException handler is present, it can override this default behavior. This handler gives the workflow host author an opportunity to provide the appropriate handling, such as custom logging, aborting the workflow, canceling the workflow, or terminating the workflow.

Applies to