Rediger

Del via


Fault Handling in a Flowchart Activity Using TryCatch

The FlowChartWithFaultHandling sample shows how the TryCatch activity can be used within a complex control flow activity.

In this sample, a promotion code and number of children are passed as variables to a Flowchart activity that calculates a discount based on formulae that correspond to the promotion code. The sample includes imperative code and workflow designer versions of the sample.

The following table details the variables for the CreateFlowchartWithFaults activity.

Parameters Description
promoCode The promotion code. Type: String

The possible values with description in parentheses:

- Single (Single)
- MNK (Married with no kids.)
- MWK (Married with kids.)
numKids The number of children. Type: int

The CreateFlowchartWithFaults activity uses a FlowSwitch<T> activity that switches on the promoCode argument and calculates the discount using the following formula.

Value of promoCode Discount (%)
Single 10
MNK 15
MWK 15 + (1 – 1/numberOfKids)*10 Note: Potentially, this calculation can throw a DivideByZeroException. So, the discount calculation is wrapped in a TryCatch activity that catches the DivideByZeroException exception and sets the discount to zero.

To use this sample

  1. Using Visual Studio, open the FlowchartWithFaultHandling.sln solution file.

  2. To build the solution, press CTRL+SHIFT+B.

  3. To run the solution, press F5.

See also