Forum Discussion

georgejecook's avatar
georgejecook
Streaming Star
5 years ago

exit for inside try catch results in unexpected behavior

I'm finding that exit for, inside of try catch give a strange stack trace. The exit for is legit, in this code, and the code behaves as expected, if the try catch block is removed


3 Replies

  • RokuJonathanD's avatar
    RokuJonathanD
    Community Moderator

    Hey George,

    What specific use case for TRY/CATCH are you working on...please advise so I can see how we can best help you.

    Thanks,

    Jonathan

  • George - this should not be happening, not in relation to `try/catch` anyhow. 

    Please provide MWE to analyze, screenshot alone was no help. 

  • Confirming - issue exists when EXIT FOR is used in FOR EACH loop nested in a TRY/CATCH statement.  Other loops (numeric FOR, WHILE) are not affected. 

    Minimalist repro: 

    Brightscript Debugger> try: for each i in [1]: ? i: exit for: end for: catch e: ? "except", e: end try
     1
    UNEXPECTED INTERNAL. (runtime error &hfe) in $LIVECOMPILE(703)

    Behavior: "Unexpected internal" error at runtime, when EXIT FOR is to be executed. 

    Notice this only happens for syntactical nesting of FOR EACH inside TRY/CATCH within the same function, it won't happen if TRY/CATCH is further down in the call stack, e.g.

    Brightscript Debugger> proc = sub(): for each i in [1,2]: ? i: exit for: end for: divZero = 1/0: end sub
    
    Brightscript Debugger> try: proc(): catch e: ? "except", e: end try
     1
    except          <Component: roAssociativeArray> =
    {
        backtrace: <Component: roArray>
        message: "Divide by Zero."
        number: 20
        rethrown: false
    }

    Workaround: avoid nesting TRY > FOR EACH > EXIT FOR within the code of a single function. Use WHILE > EXIT WHILE or FOR-TO > EXIT FOR or boolean flag where feasible. Or separate TRY/CATCH from FOR EACH > EXIT FOR with a nested call. 

    Stay tuned for a fix.