If a .net program fails to explicitely set the exit code before terminating (by calling Environment.Exit()
/ Appliation.Current.Shutdown()
/ ...), what is the exit code for that process?
Does a normal termination always result in exit code zero, and what are the other possible cases?
According to this answer to the related question Getting ExitCode From Exception Handler by Hans Passant: "if a program dies on an exception then its exit code is normally the same as the underlying exception error code".
So an uncaugth exception can cange the exit code. Is this always the case, and is the underlying exception error code always guaranteed to be different from zero, and in a specific range?
Are there other circumstances where the .net framework or Windows can automatically set another exit code, such as some non-exception related crash (is that possible?), or a forced task kill?
To put it another way, could I determine by the exit code whether the program terminated in any abnormal fashion or not?
Or if an exit code of zero can happen in some abnormal cases as well, could I include a Environment.Exit(somevalue)
in all normal termination paths for a program, and be sure that this exit code can never occur in case of a crash?
Motivation:
Since not all exeptions are catchable without severe workarounds, and since there may be other causes for sudden program termination other than uncaught excpetions, making sure that all code paths call Environment.Exit() is not alwas possible. This is why I am interested in determinining whether the exit code can be used to reliably tell whether a program exited normally.