How to terminate script execution from within DazScript?

shoei321shoei321 Posts: 113

Simple question - I need to be able to terminate execution of my script under certain conditions, for example :

if (unrecoverableErrorCondition) {
  exit();
}

But I exit() doesn't do it, nor does System.exit(), return, or any of the usual candidates. I don't see anything under DzScriptContext or DzSystem for this either. How is this best done?

Thanks

Comments

  • Richard HaseltineRichard Haseltine Posts: 96,191
    edited December 1969

    I don't know. continue would terminate any loop you were in, then you could have some kind of check to make sure execution of instructions ended -

    for ( var n = 0 ; n < limit && ! GiveUp ; n++ ) {
          if (unrecoverableErrorCondition) {
           GiveUp = true;
           continue;
         }  
    }
  • shoei321shoei321 Posts: 113
    edited December 1969

    continue and break just skip or exit the current looping structure. I'm looking to end script execution all together.

    In Java this would be System.exit(), in PHP and Perl exit(), in Python sys.exit(), etc. I can't seem to find the equivalent in DazScript, nor does it seem to be in the ECMAScript spec.

  • Richard HaseltineRichard Haseltine Posts: 96,191
    edited December 1969

    I was suggesting using continue to get out of the loop you are in, if any, and then using a boolean flag to make sure that nothing else is done - so the script should then just run do-nothing function calls until it gets to its end. Or put error checks around the actual code - I often end up with so many nested if ( we-have-the-thing-we-need) statements that in a print the actual working code would be on the right margin.

  • rbtwhizrbtwhiz Posts: 2,171
    edited November 2012

    Wrap your code in a function and exit the function early...

    function myFunction( bExitEarly )
    {
     if( bExitEarly ){
      print( "Exited the function early." );
      return;
     }
     
     print( "Exited at the end of the function." );
    };
    
    myFunction( true );

    -Rob

    Post edited by rbtwhiz on
Sign In or Register to comment.