未处理的异常终止执行时退出代码? [英] Exit Code When Unhandled Exception Terminates Execution?

查看:121
本文介绍了未处理的异常终止执行时退出代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当C#.Net控制台应用程序由于未处理的异常而终止时,是否存在确定返回哪个退出代码或是否始终使用255 的规则?



我无法找到有关此文档。一个执行并抛出新的Exception()的简单控制台应用程序退出,退出代码为255。我想知道是否可以安全地假设所有未处理的异常都将返回此错误

  C:\Temp\> ThrowsExceptionConsoleApp.exe 
C:\Temp\> echo%errorlevel%
255


解决方案

不,不总是。 255是一个Unix数字,通常由-1退出产生,不清楚您是如何看到的。在Windows上,.NET进程通常以SEH异常代码值退出,该进程使进程崩溃并终止。对于托管异常,通常为-532462766(aka 0xE0434352)。最后3个十六进制对拼写为 CCR,这是首字母缩写词,其含义在时间的迷茫中消失了,在corexcep.h中声明为EXCEPTION_COMPLUS。示例问题在这里



行为良好的程序订阅AppDomain.CurrentDomain.UnhandledException,并在调用Environment.Exit()时提供更好的退出代码,就像Marshal生成的那样。 GetHRForException()。您将获得与托管异常类型匹配的退出代码,标准异常类型的值记录在CorError.h SDK文件中。






  echo%errorlevel%

它没有在之前的问题中看到。 %errorlevel%的值只能在0到255之间,该限制可以追溯到MS-Dos天。由于SEH异常代码将始终更大,因此通常您总是会看到255。使用%errorlevel%的明智方法是假定程序不为0时失败。除非您从程序作者处获得了特定的文档。 p>

When a C# .Net console application terminates due to an unhandled exception, are there rules determining which exit code is returned or is 255 always used?

I haven't been able to find documentation on this. A simple console app that executes throw new Exception() dies with exit code 255. I'd like to know if it's safe to assume that all unhandled exceptions will return this same error code or if there are variations/corner cases I need to be aware of.

C:\Temp\> ThrowsExceptionConsoleApp.exe
C:\Temp\> echo %errorlevel%
255

解决方案

No, not always. 255 is a Unix number, normally produced by exiting with -1, unclear how you got to see that. On Windows, a .NET process normally exits with the SEH exception code value, the one that got the process to crash and terminate. Usually -532462766 (aka 0xE0434352) for a managed exception. Last 3 hex pairs spell "CCR", an acronym whose meaning is lost in the fog of time, declared as EXCEPTION_COMPLUS in corexcep.h. Sample question is here.

A well behaved program subscribes AppDomain.CurrentDomain.UnhandledException and provides a better exit code when it calls Environment.Exit(), like the one produced by Marshal.GetHRForException(). You'll get an exit code that matches the managed exception type, values for standard exception types are documented in the CorError.h SDK file.


 echo %errorlevel%

It did not see that in the question before. %errorlevel% can only have a value between 0 and 255, a restriction that goes back to the MS-Dos days. Since the SEH exception code will always be larger, you'd normally always see 255. Only sane way to use %errorlevel% is to assume the program failed when it is not 0. Unless you got specific documentation from the program author.

这篇关于未处理的异常终止执行时退出代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆