Friday, November 9, 2012

Error Handling

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
  1. First open HelloWorkFlow project from last article  Adding If / Else Logic .
  2. 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 arguments
      WorkflowInvoker.Invoke(new SayHello());
    }
    Visual Basic
    <TestMethod()>
    Public Sub ShouldHandleNullUserName()

      ' Invoking with no arguments
      WorkflowInvoker.Invoke(New SayHello())

    End Sub
  3. Now run the test. Right-click over test method name and select Run Tests.
    Run the test
    Run the test
    1.  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 failing
      ShouldHandleNullUserName test failing
      Test Result
      HelloWorkflow.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
      1. 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 Toolbox
        The TryCatch activity in the Toolbox
        Adding a TryCatch activity
        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.

          1. Now you need to move the If activity inside the Try block.
            a.       Collapse the If activity () to make the diagram smaller 
            b.       Drag-and-drop the if activity into the Try block. 

            Moving the If activity
            Moving the If activity
            1. Now you need to catch a NullReferenceException. To do this, follow these steps:
              a.       Click on Add new catch.
              b.       Select System.NullReferenceExeption from the combo box and press Enter.
              Selecting NullReferenceExeption
              Selecting NullReferenceExeption

            2. 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.
            3. 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 Basic
              New ArgumentNullException("UserName")

              Adding a Throw activity
              Adding a Throw activity
              1. 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 arguments
                 WorkflowInvoker.Invoke(new SayHello());
                }
                C#
                [TestMethod]
                [ExpectedException(typeof(ArgumentNullException))]
                public void ShouldHandleNullUserName()
                {
                 // Invoking with no arguments
                 WorkflowInvoker.Invoke(new SayHello());
                }
              2. Press CTRL+SHIFT+B to build the solution.
              3. From the menu select Test / Run / All Tests In Solution or press CTRL+R,A
              4. Verify that all the tests pass.

              3 comments:

              1. 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.

                ReplyDelete
              2. Thank you for every thing at all ; Hope be friends forever.

                ReplyDelete

              Automatic Traffic Exchange

              YallaTech Facebook page