In the previous post Adding If / Else Logic We may have noticed a potential bug in our simple application. What happens if
we don't pass a UserName to the workflow? In this article, we will add some
error handling capabilities to our workflow by using the Try/Catch, Catch<T> and Throw built-in activities.
Notice that if we 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
Notice that if we 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
- First open HelloWorkFlow project from last article Adding If / Else Logic .
- Create
a test to observe what happens when we 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.C#[TestMethod]public void ShouldHandleNullUserName(){// Invoking with no argumentsWorkflowInvoker.Invoke(new SayHello());}Visual Basic<TestMethod()>Public Sub ShouldHandleNullUserName()' Invoking with no argumentsWorkflowInvoker.Invoke(New SayHello())End Sub
- Now
run the test. Right-click over test method name and select Run Tests.Run the test
- 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.ShouldHandleNullUserName test failingTest ResultHelloWorkflow.Tests.SayHelloFixture.ShouldHandleNullUserName threw exception: System.NullReferenceException: Object reference not set to an instance of an object.
To
handle the error, We could validate the name argument prior to using it or we could simply catch the exception and deal with it. So, we will be
catching the exception.
Add caption |
- Open
SayHello.xaml in the designer and
drag a TryCatch activity from
Toolbox and drop it at the top of the sequence.The TryCatch activity in the ToolboxAdding a TryCatch activityTry/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.
b. Drag-and-drop the if activity into the Try block.
Moving the If activity - Now
you need to catch a NullReferenceException.
To do this, follow these steps:
- In the Catch section of the activity we need to decide how to handle the error. In this case, we 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.Visual BasicNew ArgumentNullException("UserName")Adding a Throw activity
- Now
we 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.C#[TestMethod][ExpectedException(typeof(ArgumentNullException))]public void ShouldHandleNullUserName(){// Invoking with no argumentsWorkflowInvoker.Invoke(new SayHello());}C#[TestMethod][ExpectedException(typeof(ArgumentNullException))]public void ShouldHandleNullUserName(){// Invoking with no argumentsWorkflowInvoker.Invoke(new SayHello());}
- Press CTRL+SHIFT+B to build the solution.
- From the menu select Test / Run / All Tests In Solution or press CTRL+R,A
- Verify that all the tests pass.
Hello Besho, This is michelle in facebook, I see you put your post in facebook every day, it is a way of popularizing your blog. but I think this way is not the most effective way. You have informative content, so you should go to the forum or connected with some useful tools that people are need. Here I want to provide you a way to get more readers. I want to invite you to review our product.our company e-iceblue.com develops office components in c#, vb.net. If you are interested in it.I think I can help you get more readers. I have three blogs, even they are in the early stage, but if these blogs has link to your blog, it is really a breakthrough for your blog as to the aspect of SEO.Also, I will add new posts regularly in my blogs. The only thing you do is to review our product, if you are so busy and do not have time, I think you can post a piece of product news on your blog.and then, there are three blogs link to your blog. think about it.please tell me you idea about it.you can find me in facebook.
ReplyDeleteThank you for every thing at all ; Hope be friends forever.
ReplyDeleteSo what do you think about my idea?
Delete