用户处理它。打开一个cmd提示窗口,运行一个程序yourblahprogram<ENTER>doecho %ERRORLEVEL%
0 表示没有错误。非零表示错误。
我将使用 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
虽然实际上上面的内容并不是那么好,因为它只有在错误级别为负时才能正常工作。'因为不是错误级别 0 表示不 >=0。正如你从 If /?来自Foolproof way to check for nonzero (error) return code in windows batch file
所以如果不是错误级别 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>
所以你可以采取行动。
windows cmd批处理脚本中除了if,还有&&和||
假设程序执行 WriteLine("asdf");并抛出异常。
这意味着运行 && 左边的第一件事,如果它没有返回错误,即错误级别 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 上会有所不同,但相似。