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

- 1
- 1

- 7,200
- 6
- 43
- 64
-
6Possible duplicate of [Exit Code When Unhandled Exception Terminates Execution?](http://stackoverflow.com/questions/35294313/exit-code-when-unhandled-exception-terminates-execution) – STLDev Apr 03 '17 at 22:12
-
@STLDevloper... this question is specific to main() function. – Pavan Chandaka Apr 03 '17 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 Apr 03 '17 at 22:14
-
Yes any how program terminates. But anything special.... when it is in main()? – Pavan Chandaka Apr 03 '17 at 22:19
-
@pavanc - No, no difference. – STLDev Apr 03 '17 at 22:21
-
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 Apr 04 '17 at 14:39
-
There is no *defined value*, because it's an unexpected condition (an unhandled exception *blasting out of main* and causing the program to abruptly terminate). It's an *unhandled exception*, which means *nothing handled it*; if nothing's handling it and it's causing an abnormal termination, why would you expect a *normal, defined return value*? Any value you get will be invalid, because it terminated abnormally and abruptly without notice or warning. – Ken White Apr 04 '17 at 17:05
-
@KenWhite what causes that value to be e.g. ` -532462766` like is there a random number generator that generates a large negative number? Or is it the value in a particular place if so, where? a cpu register? a memory location? any particular one? – barlop Apr 05 '17 at 11:01
-
@barlop: It's a meaningless value. I don't know why you and Mark are having such difficulty grasping that concept. *It's an unexpected, forced shutdown and any exit code you get from it is meaningless, because **it's from unexpected code problems that caused a forced shutdown***. IOW, who cares where it comes from because it's **absolutely meaningless** because it's caused by **unexpected termination**. It could be a corrupt memory block's content, it could be the content of two adjacent chunks of memory that have no relevance together, it could be anything, because it's **unexpected crash** – Ken White Apr 05 '17 at 12:27
-
@KenWhite I have not written anything that suggests I haven't "grasped the concept". And yeah from a practical programming perspective it doesn't matter, but from the perspective of how computers work, it's an interesting question.which memory block(in the sense that, no doubt it's not a completely random memory block), and why that one. e.g. is it the contents of the latest memory address that the program wrote to. Is it the contents of the latest one the program read from. – barlop Apr 05 '17 at 12:57
-
@barlop: *what causes that value to be X?* suggests that, because you're still looking for meaning in a meaningless value. It's a *crash*, which means the behavior is *not being controlled in a meaningful way*. It's being *force terminated*, not run through an orderly shutdown. A **crash** is called a **crash** for a reason. There is no meaning or relevance to any exit code of any value you obtain after a crash. If Windows could point it to a meaningful memory block, it would be able to handle the exception and not have to force-terminate. – Ken White Apr 05 '17 at 13:38
-
@KenWhite I never said anything about meaning. One can ask what causes it to pick the memory location that it picked. – barlop Apr 05 '17 at 15:15
-
@barlop: But you still think it **picked** something. It's a meaningless location. It wasn't *picked*, because picked would imply intent. It can be some random location in memory that was where a corrupted pointer happened to end up, or whatever happened to be at a random memory location, or the address of that corrupted pointer. *There is no meaning to the value.* That's the part you're failing to comprehend. – Ken White Apr 05 '17 at 15:47
-
@KenWhite it's so unnecessary to put words in my mouth , you keep searching for synonyms to approximate the words I use, and there is no point. I would not say intent and I don't think there's any point arguing about the definition of intent and why I didn't use the word intent and why you think what I said implies intent according to your unstated definition of intent. If it were a conscious AI system (which doesn't even exist yet by the way), then maybe that word might apply well, and I might've used it, but it doesn't and I didn't.. – barlop Apr 05 '17 at 16:20
3 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

- 36,127
- 5
- 30
- 43

- 3,193
- 1
- 18
- 19
-
1I 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`](https://learn.microsoft.com/en-us/dotnet/api/system.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`. – anion Feb 01 '23 at 16:15
-
"*I can not find any reference for that number*" - see [this answer](https://stackoverflow.com/a/35295015/65863). "*If a `StackOverflowException` ... is thrown ... then the exit-code is `-1073741571`*" - that is `STATUS_STACK_OVERFLOW` (`0xC00000FD`) – Remy Lebeau Apr 05 '23 at 22:26
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.

- 3,472
- 3
- 21
- 18
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.
-
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. – Mark Meuer Apr 04 '17 at 14:32
-
@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. – barlop Apr 05 '17 at 05:55
-
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. – Mark Meuer Apr 05 '17 at 15:14
-
@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. – barlop Apr 05 '17 at 15:19
-
True, and I do understand that. My question is that I'm looking for documentation somewhere (MS Common Language Runtime docs or something like that) that says that the exit code will be undefined. Many people are *assuming* it is random or undefined. But you and others have observed a consistent exit value on Windows of -532462766. It is unlikely to be consistent like that if it is really random. – Mark Meuer Apr 05 '17 at 15:31
-
1@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 – barlop Apr 05 '17 at 16:13