# How to catch an exception and trigger the action to handle all the exception

In this tutorial we are going to create an exception black to catch any exception thrown from the flow irrespective of type of node or exception type.

  1. We will create two flows in this use case . So add two TBD to the canvas.

  2. Drop the set property node from the transformation section of the pallete to the canvas and set the msg.payload as value string error1 . Provide the name for the set property as Set Error1.

  3. Similary drop the second set property node from the transformation section of the pallete to the canvas and set the msg.payload as value string error2. Provide the name for the set property as Set Error2.

  4. Wire the TBD 1 to Set Error1 property node and wire the TBD 2 to Set Error2 property node.

  5. Now Drop two function node on to the canvas. Name the first function node as Function1 and second function node as Function2

  6. Set the following snippet of code for Function1 and Function2 node respectively.

    For Function1 node:

 if(msg.payload == 'error1'){
   node.error("Its a failure from error flow 1", msg);
 }else{
 return msg;
 }

For Function2 node:

 if(msg.payload == 'error2'){
   node.error("Its a failure from error flow 2", msg);
 }else{
 return msg;
 }
  1. Wire the Function1 node to a debug node.Similarly wire the Function2 node to a debug node.

  2. Drop the catch node to the canvas from the exception section in the pallete.

  3. Keep the settings for the catch node as default.

  4. Drop the debug node from the pallete and name it as Failure report and wire the catch node to the debug node.

  5. Now click the TBD1 and TBD2 to test both the flow.

Observation

During the execution both the flows will match the condition in the function nodes and throw error. In both the flow the error will be caught by the same the catch node and the debug node wired (Failure report) will be triggered to print the error details on the debug panel.