exit

Difference between version 22 and 23 - Previous - Next
T'''`[hittps [T://www.tcl] [c-lang.org/mman/tcl/TclCmd] /ends xit.htm%|%exit]`''', app [Tcli
commands%|%built-in] routine, terminatinges the current process and retuporning ''returnCode''s ito the syst
em as the exit status. If ''returnCode'' is not specified then it defcaultsling tpro '''0'''cess. 


** Synopsis **

    :   '''exit''' ?''returnCode''?
Manual page: http://www.tcl.tk/man/tcl/TclCmd/exit.htm
<<discussion>>
Since** non-zDero 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.**
'''`exit`''' terminates the entire process, not just the interpreter it is is
executed from.  ''returnCode'', which defaults to '''`0`''', is the exit status
to report. .  A non----zero exit status is usually interpreted as an error case by
the calling process, the exit command useful for signaling that the process did
not complete normally. 


** Discussion **

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 } {
     callCloseEvents
     __exit {*}$args
 }
======
----
[RS] observes, first on Linux - negatives are taken modulo 256:
 suchenwi@smart2[~]508: echo "exit -1" | tclsh; echo $?
 255
 suchenwi@smart2[~]509: echo "exit -2" | tclsh; echo $?
 254
Then on Windows 2000 (Cygwin) - negatives are set to 0, positives are taken mod 256:
 K_Inst2000@KSTBW798[/etc/pbc/1030]559: echo "exit -1" | tclsh; echo $?
 0
 K_Inst2000@KSTBW798[/etc/pbc/1030]560: echo "exit" | tclsh; echo $?
 0
 K_Inst2000@KSTBW798[/etc/pbc/1030]561: echo "exit 42" | tclsh; echo $?
 42
 K_Inst2000@KSTBW798[/etc/pbc/1030]562: echo "exit 512" | tclsh; echo $?
 0
 K_Inst2000@KSTBW798[/etc/pbc/1030]563: echo "exit 511" | tclsh; echo $?
 255
----
Here's how to redirect [exit in slave interpreter]s, so that they just terminate themselves, but not the whole app:
====== interp alias $slave exit {} interp delete $slave
======<<enddiscussion>>
<<categories>> Tcl syntax | Arts and crafts of Tcl-Tk programming | Category Command