- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Jump out of a function?
Is there a way to cleanly jump out of a function? Something like the break() function in a for loop. I've tried throw() but that stops the entire JSL script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Jump out of a function?
I often enclose error prone expressions in Try() without Throw(). The placement of Try() – within the function or "around" the function call – depends if I only want to escape parts of the function or escape it completely in case of an exception. Throw() halts execution and I tend to use it mainly for debugging.
Compare these examples and their output:
//Try() inside the function escapes chosen parts of function, script execution continues.
exsqr = Function( {x},
x2 = Try( x * x, "Error, move on!" );
Show( "Show this even if x is NaN" );
x2;
);
sq = exsqr( "q" );
Show( sq, "function escape failed, but script continued" );
//Try() enclosing the function call escapes function, script execution continues.
exsqr = Function( {x},
x2 = x * x;
Show( "Don't show this if x is NaN" );
x2;
);
sq = Try( exsqr( "q" ) );
Show( sq, "function escape succeeded!" );
//Try() with Throw() escapes function and script.
exsqr = Function( {x},
x2 = Try( x * x, Throw( "Error, stop here!: " || Char( exception_msg ) ) );
Show( "Don't show this if x is NaN" );
);
sq = exsqr( "q" );
Show( sq, "script stopped!" );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Jump out of a function?
Could you use return()?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Jump out of a function?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Jump out of a function?
I am getting an error when trying to exit a function with return().
I get the error when I make a button box
bb = Button Box("SpecsAuto")
then attach a function
bb << Set Function( Return() )
then click the box.
Here is demo code to reproduce the error:
nw = New Window("Win1");
nw << Append(bb = Button Box("SpecsAuto"));
bb << Set Function(
Print("A");
Return();
Print("B");
);
bb << Click();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Jump out of a function?
set function() also works if no function is provided - like in your script.
To use the magic of return(), you have to put it inside a function:
nw = New Window("Win1");
nw << Append(bb = Button Box("SpecsAuto"));
bb << Set function(Function({},
Print("A");
Return();
Print("B");)
);
bb << Click();