10

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)?

14
  • 6
    Possible duplicate of Exit Code When Unhandled Exception Terminates Execution? Commented Apr 3, 2017 at 22:12
  • @STLDevloper... this question is specific to main() function. Commented Apr 3, 2017 at 22:14
  • 4
    There'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. Commented Apr 3, 2017 at 22:14
  • Yes any how program terminates. But anything special.... when it is in main()? Commented 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? Commented Apr 4, 2017 at 14:39

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

I can not find any reference for that number on MSDN. So i guess we do not know for sure if this is always the same value in every .NET-version on any OS. However: I have tested it on a Windows 10 machine using .NET6 and got always the same result-number even with different exception-types, but with one special case: If a 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.
"I can not find any reference for that number" - see this answer. "If a StackOverflowException ... is thrown ... then the exit-code is -1073741571" - that is STATUS_STACK_OVERFLOW (0xC00000FD)
1

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

1

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 exitcodes, for different exceptions, is defined as NTSTATUS
Only took me a little more than 8 years to find the answer!
-2

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.

6 Comments

Thank you, @barlop. I do understand that the user can deal with the different codes returned by the program to the shell, and I appreciate your explanations. However, I'm looking to understand if there is a standard definition of what that code will be when an exception occurs. I was not asking what to do with that code.
@MarkMeuer there may be a standard of 0 for no error, and non-zero or negative for error.. but i'm not sure where that's written. And if that's really what you are asking then you wrote your question extremely badly, asking what happens to the error code.
I have updated my question to try to clarify. I'm sorry if I caused you extra work in your answer. I am simply looking for documentation on what the exit value will be when an exception is thrown out of main in a C# program.
@MarkMeuer well if it's outputting the contents of some memory location, then there wouldn't be any particular value, other than whatever value happens to be in that memory location, which could potentially be anything.
@MarkMeuer interesting point.. I did get the same value as jeffrey got, and yeah it'd be interesting if any documentation said undefined or that it should be negative or something about it.. I think your question is a good one
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.