Forum Discussion

Anton_A's avatar
Anton_A
Occasional Contributor
14 years ago

Doesn't return anything from catch block

Hi,



Could someone pls see my function on JScript. I am looking for particular node in infragistic tree and want to handle case when there is no such node inside.



function SelectNode(treeObj, soughtNode) {

var node = FindNode(treeObj, soughtNode); //if sought value is not found i expect here stub object, but it is undefined actually

if (node.Exists)

node.set_Selected(true);

}



function FindNode(treeObj, soughtNode) {

try {

   for (var i = 0; i < treeObj.Nodes.Count; i++) {

      var node = treeObj.Nodes.Item(i);

     if(SameText(soughtNode, node.Text.OleValue)) {

     return node;

     }

   }

   FindNode(node, soughtNode);

   } catch(error) {

   Log.Warning(error.name,error.description);

   return Utils.CreateStubObject();

   }  

}
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    The try/catch logic will only fire if there is some sort of exception generated.  However, in your code, if the node doesn't exist, it won't generate an error.  In order to hit your catch block, you'll need to "throw" an error.
  • Hi,



    Also, code which is executed when there's no expected node goes right after your loop. There's no need to use try/catch at all. The loop can either jump to the caller function (via return), or finish normally if it doesn't find the needed node according to your current code.
  • Anton_A's avatar
    Anton_A
    Occasional Contributor
    Robert, Jared, thanks for your replies!



    Anyway that doesn't help me because i use recursion in this code and when it tries to obtain control under not existed node it throws exception (as you may see in the attach -  without_try_catch.JPG). The catch block is actually achieved and stub object assigned to node as well (as you may see in the attach - error_catch.JPG).You can also see that I log warning in catch block and this action performs successfully. But function doesn't return this stub object  in finally block (i've changed a bit the function for better show the issue, please see - return_undefined.JPG).  Moreover, when i add finally block to the function and now debugging it's assigned wrong node in finally block (see wrong_object_assigned.JPG ). I wonder how this happens if catch block were achieved. All screenshots were made consequentially in single debug action. Thanks in advance, Anton.
  • Anton_A's avatar
    Anton_A
    Occasional Contributor
    Regarding previous posts, the error was fired because the recursion was placed outside of  loop.

    Below is another version of this function, but it doesn't work as well. It call return twice. You can see sequential debug steps in the attached screenshots. Thanks, Anton.



    function FindNode(treeObj, soughtNode) {

       for (var i = 0; i < treeObj.Nodes.Count; i++) {

          var node = treeObj.Nodes.Item(i);

         if(SameText(soughtNode, node.Text.OleValue)) {

         return node;

         }

         FindNode(node, soughtNode);

       } return Utils.CreateStubObject();

     }
  • Hi,



    Actually, you need to check k,your function's return value before using it. Also, you should obtain the subsequent call's result. I'd rewrite your code like this:



    function FindNode(treeObj, soughtNode)

    {

    for (var i = 0; i < treeObj.Nodes.Count; i++)

    {

    var node = treeObj.Nodes.Item(i);



    if(SameText(soughtNode, node.Text.OleValue))

    {

    return node;

    }



    node = FindNode(node, soughtNode);

    if(!node) break;

    }



    Log.Warning("Node not found");

    return null;

    }

    ...

    var node = FindNode(treeObj, soughtNode);

    if(node) node.set_Selected(true);

    ...
  • Anton_A's avatar
    Anton_A
    Occasional Contributor
    Hi Jared. Your solution doesn't work the same as my previous because of wrong recursion handling. I've checked it in debug mode, it doesn't return finded node, instead it finishes loop, then post warning and return null. My suggestion is that parent FindNode() functions isn't closed by sub function's return. But i have found the solution and it is working, hope that will help somebody else:



    function FindNode(treeObjs, soughtNode) {

       for (var i = 0; i < treeObjs.Count; i++)

       {

         var node = treeObjs.Item(i);

         if(SameText(node.Text.OleValue,soughtNode))

            return node;

         return FindNode(node.Nodes, soughtNode);             

       }

    }



    ...



    function VerifyNodeExists(treeObj, soughtNode) {

    var node = FindNode(treeObj.Nodes, soughtNode);

    if (node!=undefined )

    Log.Checkpoint("Sought node '"+soughtNode+"' was found");

    else Log.Warning("Couldn't find '"+soughtNode+"' node");

    }
  • Anton_A's avatar
    Anton_A
    Occasional Contributor
    Previous solution doesn't work for all items in collection. This is my last working copy. Jared, thanks.



    function FindNode(node, soughtNode)

    {   

      if(SameText(node.Text.OleValue,soughtNode))

        return node;

             

       for (var i = 0; i < node.Nodes.Count; i++)

       {                      

          found = FindNode(node.Nodes.Item(i), soughtNode);     

          if(found != null)

            return found;

       }

               

       return null;

    }