Branching out of a subroutine

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Liftman
Newbie
Posts: 2
Joined: Fri May 12, 2017 3:15 pm

Branching out of a subroutine

Post by Liftman » Fri May 12, 2017 3:32 pm

Hi,
I wish to detect undesirable situations and run a subroutine to deal with them. I thought I could utilise the custom OnEvent command to deal with this but I often encounter the 'branching out of a subroutine' error. My script has a number of subroutines, many of which are nested. Is there a way of doing this without having to insert handlers in every subroutine?

Liftman
Newbie
Posts: 2
Joined: Fri May 12, 2017 3:15 pm

Re: Branching out of a subroutine

Post by Liftman » Mon Jun 26, 2017 5:30 pm

Anyone?

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Branching out of a subroutine

Post by JRL » Wed Jun 28, 2017 1:33 pm

Is there a way of doing this without having to insert handlers in every subroutine?
Not that I know of. But typically you get the branching out of a subroutine error when using Goto> and labels. sounds like you perhaps have Goto>s in your OnEvent> subroutine(s) that branch out of the nested subroutines?

User avatar
Phil Pendlebury
Automation Wizard
Posts: 538
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Branching out of a subroutine

Post by Phil Pendlebury » Thu Jul 20, 2017 8:40 am

I think SkipLabel might be similar to what you need.

I had to do this a while ago and cannot remember if that was the exact command I used. I will search my scripts and see if I can verify...

Yes it is Skiplabel. Only downside is it doesn't work with OnEvent subs.

From the doc:
SkipLabel>LabelName

SkipLabel is used within a subroutine to tell the script to jump to a label after the subroutine has ended. It is bad practice to jump out of a subroutine using a Goto statement - as the subroutine may then never end, causing problems with, for example, OnEvent handlers. In some cases however it is useful to cause the script to jump to a label once the subroutine has finished. This is what SkipLabel does.

Note that SkipLabel is NOT supported by OnEvent subroutines.

Example

Code: Select all

Let>x=0
Repeat>x
  Let>x=x+1
  GoSub>ExampleSubroutine
Until>x=100

SRT>ExampleSubroutine
  If>x=5
    SkipLabel>EndScript
  Endif
  //this bit still gets executed
  Let>y=x
END>ExampleSubroutine

Label>EndScript

MessageModal>y
Phil Pendlebury - Linktree

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts