What is the exit code for a C # program that throws an exception from the main one?

In a C # program, what is the exit code defined as an exception thrown from main? I know that you can set the exit code in several ways, as described in this excellent answer . But I am very surprised that I can not find the documentation for which the value of the exit code is determined when the exception is thrown from the main one. Is there a standard that defines what the meaning of the exit code will mean in this case, or depends on the operating system (or chance or something else)?

+4
source share
3 answers

I am observing -532462766

PS C:\Projects\Throw\Throw\bin\Debug> .\Throw.exe

: System.Exception: "System.Exception" Throw.Program.Main(String [] args) c:\Projects\Throw\Throw\Program.cs: 13

PS C:\Projects\Throw\Throw\bin\Debug> $LASTEXITCODE
-532462766
0

, . Zero " ". Dot net Windows, , ( , ). Linux/BSD (, MAC), , 255 (IIRC, " , , - ".

, - , error code != 0 .

0

. cmd, yourblahprogram<ENTER> do echo %ERRORLEVEL%

0 . non zero .

DIR,

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>

C:\Users\user>if NOT ERRORLEVEL 0  echo asdf

, , . 'cos NOT ERRORLEVEL 0 not >= 0. If/? () Windows

, errorlevel 0. IF %ERRORLEVEL% NEQ 0 echo asdf

, 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>

, .

if windows cmd batch script, && ||

, , WriteLine ( "asdf" ); .

, , && , errorlevel 0, , . , , echo qwerty, qwerty.

C:\Users\user>a.exe && echo qwerty
asdf

Unhandled Exception: System.Exception: Exception of type 'System.Exception' was thrown.
   at CustomMath.Main()

. || OR, , , , , , , . , , 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>

, .

, , linux.

-1

Source: https://habr.com/ru/post/1673931/


All Articles