I. faulthandler
The following figure shows how to add a workflow:
First, add a seruence, add three codes in it, add a code outside, enable seruence error handling, add a faulthandler in the container, and add a code activity in faulthandler.
Set the faultType attribute of faulthandler as follows:
Be sure to bind the property.
Running effect:
Note that if faulthandler captures errors, other code activities in seruence will not be executed, but the code outside seruence will continue to be executed.
Ii. Throw
In the WWF workflow, throw can also throw an exception with specific information. Similar to the throw of C. You can declare an explicit exception in the workflow through the throw activity and throw it. When an exception occurs, the "workflowterminated" event of the container runtime when the workflow is running is triggered.
Create a workflow as follows:
Set the faultType attribute of the throw activity as follows:
The Code is as follows:
Public sealed partial class workflow1: sequentialworkflowactivity {public workflow1 () {initializecomponent ();} private void code1 (Object sender, eventargs e) {console. writeline ("code1 execution... "); console. readkey ();} private void code2 (Object sender, eventargs e) {console. writeline ("caught \" try to divide by 0 \ "exception ");}}
The output is as follows:
If the sequence container does not capture exceptions, it is easy that the subsequent codeacticity1 activity will not be executed.
Iii. Compensate
In actual work, the workflow also needs to process the business logic in some special circumstances. For example, if an email is sent incorrectly, It is not deleted by logging on to the customer's mailbox, instead, send another email to inform the user of the error.
In WWF, you can use the compensatablesequence activity and compensatable activity.
Like a faulthandler activity, compensation also needs to specify a region. If exceptions occur in this region, compensatable can be used to specify the region in which the exception type should be compensated. The compensatablesequence activity can be used in this region. The compensatablesequence activity is an easy sequence type, similar to the sequence activity.
Example: If a mode user sends a parcel to a purchased item, and the delivery delay may be caused by weather during transportation, the compensation process will be started to notify the buyer.
WWF transaction and exception handling activities <Part 4>