Partilhar via


Throw Sample

Download sample

Exceptions provide a structured, uniform, and type-safe way of handling workflow, scope, and user-level error conditions. The exception mechanism in Windows Workflow Foundation is similar to C#. It enables a developer to throw exceptions, catch exceptions by type, and assign exceptions to a variable.

This sample shows how a workflow can model and throw an exception, and how this exception is processed by the hosting application. The sample uses a ThrowActivity activity to throw an Exception. Because no exception handlers are defined in the workflow, the exception escapes the workflow and the workflow is terminated. Because the workflow is executed asynchronously, the exception information is sent to the host application through the WorkflowTerminatedEventArgs in the WorkflowTerminated event.

When you use the ThrowActivity activity, you have the option of specifying the type of the exception to throw and assigning that type to the FaultType property or by instantiating an exception object and assigning that instance to the Fault property. When you use the FaultType property, the ThrowActivity activity will create an instance of that exception type when it throws the exception. You would use this method when you want to throw a generic exception or when using a fully declarative authoring scenario. To provide additional and more concrete exception information, create an instance of an exception object and assign it to the Fault property of the ThrowActivity activity. This sample demonstrates this method.

Note

Using the ThrowActivity activity is effectively the same as throwing an exception from a code handler or from the execution code in an activity; however, using the ThrowActivity activity gives the workflow author the ability to explicitly model exception throwing in a workflow.

To build the sample

  1. Download the sample by clicking Download Sample in this topic.

    This extracts the sample project to your local hard disk.

  2. Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.

  3. Go to the source directory of the sample.

  4. At the command prompt, type MSBUILD <Solution file name>.

To run the sample

  1. In the SDK Command Prompt window, run the .exe file in the Throw\bin\debug folder (or the Throw\bin folder for the Visual Basic version of the sample), which is located below the main folder for the sample.

See Also

Reference

ThrowActivity

Other Resources

Using the ThrowActivity Activity
Nested Exception Handlers Sample
Fault Handling in Workflows
Activities Samples
Windows Workflow Foundation Samples

© 2007 Microsoft Corporation. All rights reserved.