Updated 2015-07-14 20:05:59 by kap

This Tcl command ends the application, terminating the process and returning returnCode to the system as the exit status. If returnCode is not specified then it defaults to 0.
exit ?returnCode?

Manual page: http://www.tcl.tk/man/tcl/TclCmd/exit.htm

Since non-zero exit codes are usually interpreted as error cases by the calling process, the exit command is an important part of signaling that something fatal has gone wrong.

Takes an optional return code (small integer, 0..127) as argument.

LV does exit itself limit the return value to 0-127, or is it the errorCode Magic Name that is doing the limiting?

From comp.lang.tcl:

Andreas Otto reported "exit 999" to return 231 as a bug.

Pat Thoyts: I notice that 999 is 0x03e7 and 231 is 0x00e7 so provided you restrict exit codes to a maximum of 255 everything should be ok.

Donal Fellows: However it is not a good idea to use exit codes larger than 127, as 128 and higher are used to represent exits due to (unhandled) signals on UNIX. Mind you, if you need more than 128 different exit codes, you've probably got the wrong architecture of software... :^)

Oh, come on:
 rename exit __exit
 proc exit { args } {
    set code [ lindex $args 0 ]
    if { $code == 12345678910111213 } { __exit }
    puts stderr "you and what army?"

It's Tcl guys, remember??

I also noticed that exit is being called when the program ends even if you do not call exit. this can be useful if you want a certain event happen before the exit:
 rename exit __exit
 proc exit { args } {
     __exit {*}$args

RS observes, first on Linux - negatives are taken modulo 256:
 [email protected][~]508: echo "exit -1" | tclsh; echo $?
 [email protected][~]509: echo "exit -2" | tclsh; echo $?

Then on Windows 2000 (Cygwin) - negatives are set to 0, positives are taken mod 256:
 [email protected][/etc/pbc/1030]559: echo "exit -1" | tclsh; echo $?
 [email protected][/etc/pbc/1030]560: echo "exit" | tclsh; echo $?
 [email protected][/etc/pbc/1030]561: echo "exit 42" | tclsh; echo $?
 [email protected][/etc/pbc/1030]562: echo "exit 512" | tclsh; echo $?
 [email protected][/etc/pbc/1030]563: echo "exit 511" | tclsh; echo $?

Here's how to redirect exit in slave interpreters, so that they just terminate themselves, but not the whole app:
 interp alias $slave exit {} interp delete $slave