jackxiao / jslibs

Automatically exported from code.google.com/p/jslibs
0 stars 0 forks source link

Exit function for gracefully stoping the script #69

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Halt() is a non-graceful method of exiting a script.

Currently the method of exiting the script and returning a response code,
is to run the script to the very end, and place an integer at the end of
the code (the last thing evaluated in the script is treated as the response
code if it is an integer).

{{{
// start
var exitCode = 0;
if( test is ok ) {
  ... rest of app ...
} else exitCode = 1;
exitCode; // Exit code
// end
}}}

However other languages make use of a graceful exit function to allow the
script to be exited from any location cleanly while returning the response
code.

{{{
<?php
if( test is not ok ) {
  echo "bad stuff...";
  exit 1;
  // A simpler common usage is die("Bad stuff...");
}

... rest of app ...

// no exit line, default is 0
}}}

Note that I am only asking for a Exit function or the like. Perhaps
something like.
Exit(code);
Exit(); // defaults to 0

I'd say please don't use 'exit', or 'die'. I'm writing a series of
libraries in JavaScript to improve the higher levels of coding.
exit;
exit=1; // it's not possible to do exit 1; so this is what I did in place.
die("string...");
Are all methods I've setup based on a Exit(code) which currently uses
Halt(); as a temporary use.

Just creating an Exit(code); would be fine enough and I could remove the
Exit I defined that uses Halt();

Original issue reported on code.google.com by nadir.se...@gmail.com on 11 Nov 2008 at 10:57

GoogleCodeExporter commented 9 years ago
I think there is no way to "graceful exit" a JavaScript program other than its 
proper
end.
However, I will try to implement an Exit(<code>) function that will throw an 
Exit
exception.
If the Exit exception is not caught, the program will use the <code> as exit 
code.

Original comment by sou...@gmail.com on 14 Nov 2008 at 6:27

GoogleCodeExporter commented 9 years ago
Finally, I will simply use the valueOf any uncatched exception as exit code.
eg.
throw 2

Original comment by sou...@gmail.com on 16 Nov 2008 at 8:19

GoogleCodeExporter commented 9 years ago
fixed at revision r2157.

Original comment by sou...@gmail.com on 16 Nov 2008 at 8:40

GoogleCodeExporter commented 9 years ago
Whohoo...

Ok, so did you create Exit, or just change exception handling?

Just so I know if I should tweak my Exit function or remove it.

Original comment by nadir.se...@gmail.com on 16 Nov 2008 at 10:10

GoogleCodeExporter commented 9 years ago
Just change the exception handling. You can change your Exit() function like 
this:
function Exit(exitCode) {
  throw exitCode;
}

Perhaps I will remove the Halt() function.

Original comment by sou...@gmail.com on 16 Nov 2008 at 10:22