Exercise 8: Error Handling
You may have noticed a potential bug in your simple application. What happens if you do not pass a UserName to the workflow? In this exercise, you will add some error handling capabilities to your workflow by using the Try/Catch, Catch<T> and Throw built-in activities.
Task 0 – Opening the Solution
To begin this exercise you can use the solution you finished from Exercise 7. Alternatively, you can follow the following steps to begin with Exercise 8.
- Start Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010.
- Open the starting solution for Exercise 8 located under the Source\Ex8-ErrorHandling\Begin folder and use it as the starting point for this exercise.
- Press CTRL+SHIFT+B to build the solution.
Task 1 – Writing a Test to Observe Error Behavior
Notice that if you pass an empty string ("") the application should function correctly. The only way to get a null name passed to the workflow is to create it without supplying the in argument for UserName.
Create a test to observe what happens when you do not pass the name argument to the workflow. To do this, using Solution Explorer, open SayHelloFixture.cs (C#) or SayHelloFixture.vb (Visual Basic) located under the HelloWorkflow.Tests project and add the following test.
(Code Snippet-Introduction to WF4Lab-ShouldHandleNullUserName Test CSharp)
[TestMethod] public void ShouldHandleNullUserName() { // Invoking with no arguments WorkflowInvoker.Invoke(new SayHello()); }
(Code Snippet-Introduction to WF4Lab-ShouldHandleNullUserName Test VB)
<TestMethod()> Public Sub ShouldHandleNullUserName() ' Invoking with no arguments WorkflowInvoker.Invoke(New SayHello()) End Sub
FakePre-bb2f72c170034bc1845ba0aca0bf2260-2c4d9572c1ea4911ad472f41fc67b152
Now run the test. Press Ctrl+R, T or Right-click over test method name and select Run Tests ().
The result is that the test fails with a NullReferenceException because you have an expression that uses UserName.Length in the If activity and UserName was null.
Figure 49
ShouldHandleNullUserName test failing
HelloWorkflow.Tests.SayHelloFixture.ShouldHandleNullUserName threw exception: System.NullReferenceException: Object reference not set to an instance of an object.
Task 2 – Adding the Try/Catch Activity to the Workflow
To handle the error, you could validate the name argument prior to using it or you could simply catch the exception and deal with it. In this task, you will be catching the exception.
Open SayHello.xaml in the designer and drag a TryCatch activity from Toolbox and drop it at the top of the sequence.
Figure 50
The TryCatch activity in the Toolbox
Figure 51
Adding a TryCatch activity
Try/Catch Activity
Workflows can use the
TryCatch activity to handle exceptions that are raised during the execution of a workflow. These exceptions can be handled or they can be re-thrown using the
Throw activity. Activities in the
Finally section are executed when either the
Try section or the Catches section completes.
Now you need to move the If activity inside the Try block.
- Collapse the If activity () to make the diagram smaller
- Drag-and-drop the if activity into the Try block.
Figure 52
Moving the If activity
- Now you need to catch a NullReferenceException. To do this, follow these steps:
- Click on Add new catch.
Select System.NullReferenceExeption from the combo box and press Enter
Figure 53
Selecting NullReferenceExeption
- In the Catch section of the activity you need to decide how to handle the error. In this case, you are going to replace the exception with an ArgumentNullException and throw it. This lets the caller know that the exception was their fault for not supplying the UserName argument. Drag a Throw activity from the Toolbox and drop it into the Catches area.
Set the Throw activity expression. To do this, select the Throw activity and in the Properties windows type the following expression in the Exception box.
New ArgumentNullException("UserName")
Figure 54
Adding a Throw activity
Now you need to fix the ShouldHandleNullUserName test so that it expects this exception. To do this, open SayHelloFixture.cs and add an ExpectedException annotation to the test method as shown in the following code.
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]FakePre-aab276cc369a4f9091a8208d05648b4e-0a5dd30bf4c44b26a451e8cc18af0c0cFakePre-a73491eb79bb413aa25c76e71b1d140c-85356d6cba4147bd88d564478c2fe03eFakePre-5cced26525644c729e1c535b5d7ade1e-c38b5a8425d440ca9ae120ebf75b4ff6FakePre-a29041e34f574dd89fe6850c9ff9a632-e8c12e1c159647f48554cbd5c51e61edFakePre-caa2d9d4c660454887cb3e4f6d66e6e2-5497fafd78254b49b2a4c170967fe9bd
<TestMethod(), ExpectedException(GetType(ArgumentNullException))>
Public Sub ShouldHandleNullUserName()
FakePre-2aeb02302c5c430f91e46fe6b04f534c-46e8ef76adf545a9afb71a77da8335dfFakePre-7f48bf70144c406b830953586160e4ae-360d3149996d4900aefaa77c5eff2d51FakePre-f63366283dc74d5fb33516b8ca7344e6-dd28a06c0e774799af32f4af774bbc2cFakePre-8fe7dacf0bec41be9a55ed73ba84fc3a-193c49a3c8c9421792707b3e61ed9e00FakePre-c213b2aaf6824d48904d4d366695cb9b-323eb4d1950f4a2780c98bb17d686342
Next Step
Exercise 8: Verification