Work with Exception Handling in Workflow


Introduction : An Exception is an event which occurs during the execution of program, that disrupts the normal flow of the programs' instructions. When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

How to create FlowDecision activity:

Step : 1 Open Visual Studio->File ->right-click.

  • Select New->Project option.
  • Select Workflow Console Application.
workflow.gif

Define Class :

Step : 2 Go to Solution Explorer and right-click.

  • Select->Add->New Item.
  • Select Class.
  • Write a code.
class.gif

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;|
namespace WorkflowConsoleApplication2
{
     public class or1item        
     {
         public int item { get; set; }
         public int quantity { get; set; }
         public string code { get; set; }
         public string description { get; set; }
     }
     public class or
     {
         public or()
         {
             item = new List<or1item>();
           
         }
         public int id { get; set; }
         public string description { get; set; }
         public decimal weight { get; set; }
         public string shippingMethod  { get; set; }
         public List<or1item> item { get; set; }
    }
    public class STOCKException:Exception
    {
        public STOCKException():base()
        {

        }
        public STOCKException(string message):base (message)
        {

        }
    }
}

Design the Workflow.

Step : 3 Go to Workflow.xaml option.

  • Select View Designer.
  • Drag activity from Toolbox.
  • Drag Sequence, Assign, WriteLine, Switch<T>activity.
solution5.gif

Step : 4 Go to Solution Explorer and right-click

  • Select Workflow1.xaml ->View code.
  • Write the below code.

Code :

<Switchx:TypeArguments="x:String"Expression="orderinfo.ShippingMethod"sap:VirtualizedContainerService.HintSize="472,151">
      <Switch.Default>
        <
Add x:TypeArguments="x:Decimal, x:Decimal, x:Decimal" DisplayName="Add 19" Left="[amount]" Resu
[amount]" Right="[8.0D]"
/>
      </Switch.Default>
      <
Add x:TypeArguments="x:Decimal, x:Decimal, x:Decimal" x:Key="day" DisplayName="Add 19" sap:VirtualizedContainerService.HintSize="452,100" Left="[amount]" Result="[amount]" Right="[18.0D]" />
<Add x:TypeArguments="x:Decimal, x:Decimal, x:Decimal" x:Key="firstday" DisplayName="Add 14"sap:VirtualizedContainerService.HintSize="452,100" Left="[amount]" Result="[amount]" Right="[20.0D]" /> </Switch>
    <
Assign sap:VirtualizedContainerService.HintSize="472,57">
      <Assign.To>
        <
OutArgument x:TypeArguments="x:Decimal">[amount]</OutArgument>
      </Assign.To>
      <
Assign.Value>
  <
InArgument x:TypeArguments="x:Decimal">[amount + (orderinfo.weight * 0.5D)]</InArgument>
      </Assign.Value>
    </
Assign>
    <
WriteLine sap:VirtualizedContainerService.HintSize="472,59" Text="[&quot;th amount is :&quot;
+amount.ToString()]"
/>
 </Sequence>

Step : 5 Select the Assign activity and set the DisplayName and Value.

Value :

"The item total is: $" + TotalAmount.ToString()

Invoking the Workflow :

Code :
using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
using System.Collections.Generic;
namespace WorkflowConsoleApplication2
{
 class Program
{
        static void Main(string[] args)
        {
        or myor = new or
        {
            id = 1,
            description = "stuff",
            shippingMethod = "firstday",
            weight = 120,
        };
        IDictionary<string, object> input = new Dictionary<string, object>
            {
                {"orderinfo", myor}
            };
            IDictionary<string,object>output = WorkflowInvoker.Invoke(new or1WF(), input);
            decimal total=(decimal)output["amount"];
            Console.WriteLine("workflow return${0} for myor total",total);
            Console.WriteLine("press enter");
            Console.ReadLine();
        }
    }
}

Step : 6 Now we drag a ForEach Activity from the Toolbox.

  • Click the expand link and write the below expression.

Expression :

orderinfo.items

Now see the Exception Handling :

Step :7 Go to Visual studio.

  • Select File->Project option.

  • Select Blank Solution.

solution1.gif

Step : 8
Go to Solution Explorer and right-click.
  • Select ADD->Existing item.

  • Workflow Console Application.csj project add.

  • Add Existing Project.

solution2.gif

Step : 9
Drag Throw Activity From Toolbox.

  • Write a expression.

    try-catch.gif

Expression :

New OrderProcess.OutOfStockException("Item Code: " + item.ItemCode)

Step : 10 Now press F5 to run.         

result1.gif