In a C# program, what is the exit code defined to be when an exception in thrown out of main? I know you can set the exit code in a number of ways as documented in this excellent answer. But I'm very surprised that I can't find documentation for what the value of the exit code is defined to be when an exception is thrown out of main. Is there a standard that defines what the value of the exit code will be in this case, or does it depend upon the operating system (or chance, or anything else)?
-
6Possible duplicate of Exit Code When Unhandled Exception Terminates Execution?STLDev– STLDev2017-04-03 22:12:54 +00:00Commented Apr 3, 2017 at 22:12
-
@STLDevloper... this question is specific to main() function.Pavan Chandaka– Pavan Chandaka2017-04-03 22:14:02 +00:00Commented Apr 3, 2017 at 22:14
-
4There's no difference between being kicked out in main() and being kicked out somewhere else. And any exit code is invalid anyway; if it's anything but success or failure that you've set yourself, it's meaningless. It it matters to you, don't let the exception escape and set it yourself and then exit cleanly.Ken White– Ken White2017-04-03 22:14:31 +00:00Commented Apr 3, 2017 at 22:14
-
Yes any how program terminates. But anything special.... when it is in main()?Pavan Chandaka– Pavan Chandaka2017-04-03 22:19:08 +00:00Commented Apr 3, 2017 at 22:19
-
1@KenWhite That is exactly my question: When an exception blasts out of Main and terminates the program, is there a defined value that I can check for with confidence, or will it be some random or undefined value, thus forcing me to catch the exception and set the return value explicitly? Can you point me at any documentation that says what the return value will be when the main terminates because of an exception?Mark Meuer– Mark Meuer2017-04-04 14:39:21 +00:00Commented Apr 4, 2017 at 14:39
4 Answers
I observe -532462766
PS C:\Projects\Throw\Throw\bin\Debug> .\Throw.exe
Unhandled Exception: System.Exception: Exception of type 'System.Exception' was thrown at Throw.Program.Main(String[] args) in c:\Projects\Throw\Throw\Program.cs:line 13
PS C:\Projects\Throw\Throw\bin\Debug> $LASTEXITCODE
-532462766
2 Comments
StackOverflowException (which is caused by a stackoverflow, not not by throwing it manually) is thrown out of the main-function then the exit-code is -1073741571.StackOverflowException ... is thrown ... then the exit-code is -1073741571" - that is STATUS_STACK_OVERFLOW (0xC00000FD)If you are looking for a successful exit, I would look for only zero. Zero is the standard "everything was good" code. In Dot net for Windows, from my experience, any non - handled exceptions will cause a negative exit code (I believe the number is based on the exception type). There are no negative exit codes in Linux / BSD (aka MAC), so I would assume that unhandled exceptions are 255 (IIRC, the "We don't know exactly broke, but something did break" code.
So, if you are looking for a cross - platform solution, just consider error code != 0 a failure.
Comments
The C# language specification section 7.2 states:
If the effective entry point method terminates due to an exception (§21.4), the exit code is implementation-defined. Additionally, the implementation may provide alternative APIs for specifying the exit code.
That explains why we see consistent exit code values in Windows, and that they are different than those seen in Linux or other OS's. Each implementation is allowed to determine what the code will be.
2 Comments
The user handles it. Open a cmd prompt window, run a program yourblahprogram<ENTER> do echo %ERRORLEVEL%
0 signifies no error. non zero indicates error.
i'll use DIR but you can use your program
C:\Users\user\aa\a>dir
Volume in drive C has no label.
Volume Serial Number is B411-D580
Directory of C:\Users\user\aa\a
03/04/2017 11:11 PM <DIR> .
03/04/2017 11:11 PM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 15,943,544,832 bytes free
C:\Users\user\aa\a>echo %ERRORLEVEL%
0
C:\Users\user\aa\a>dir sdsklfjdlkdfjs
Volume in drive C has no label.
Volume Serial Number is B411-D580
Directory of C:\Users\user\aa\a
File Not Found
C:\Users\user\aa\a>echo %ERRORLEVEL%
1
C:\Users\user\aa\a>
And you can act based on it
C:\Users\user>if NOT ERRORLEVEL 0 echo asdf
though actually teh above isn't that good 'cos it only works well when the errorlevel is negative.. 'cos NOT ERRORLEVEL 0 means not >=0. As you see from If /? and from Foolproof way to check for nonzero (error) return code in windows batch file
so this would be better if not errorlevel 0. IF %ERRORLEVEL% NEQ 0 echo asdf
so if your code does throw new System.Exception();
C:\Users\user>a.exe
asdf
Unhandled Exception: System.Exception: Exception of type 'System.Exception' was thrown.
at CustomMath.Main()
C:\Users\user>echo %ERRORLEVEL%
-532462766
C:\Users\harvey>
So you can act on that.
Besides if in windows cmd batch script, there is also && and ||
So let's say the program does WriteLine("asdf"); and throws an exception.
This means run the first thing to the left of the && and if it returns no error i.e. errorlevel 0, then run second thing, to the right. So you see that first example doesn't run echo qwerty, it doesn't display qwerty.
C:\Users\user>a.exe && echo qwerty
asdf
Unhandled Exception: System.Exception: Exception of type 'System.Exception' was thrown.
at CustomMath.Main()
Now look at the second example. The || is OR, So it will run if either the left hand side or the right hand side is true, but it will be efficient so as soon as one of the sides is true it will complete. The left hand side runs first, and fails, so it continues and runs the right hand side, and will show qwerty.
C:\Users\user>a.exe || echo qwerty
asdf
Unhandled Exception: System.Exception: Exception of type 'System.Exception' was thrown.
at CustomMath.Main()
qwerty
C:\Users\user>
So, you use your OS to handle it.
It'd be different, but similar, on linux.