C# 什么是 Environment.FailFast?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/564581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is Environment.FailFast?
提问by Embedd_Khurja
What is Environment.FailFast?
什么是 Environment.FailFast?
How is it useful?
它如何有用?
采纳答案by cgreeno
It is used to kill an application, its a static method that will instantly kill an application without being caught by any exception blocks.
它用于杀死应用程序,它是一种静态方法,可以立即杀死应用程序而不会被任何异常块捕获。
Environment.FastFail(String) can actually be a great debugging tool. For example, say you have an application that is just downright giving you some weird output. You have no idea why. You know it's wrong, but there are just no exceptions bubbling to the surface to help you out. Well, if you have access to Visual Studio 2005's Debug->Exceptions... menu item, you can actually tell Visual Studio to allow you to see those first chance exceptions. If you don't have that, however you can put Environment.FastFail(String) in an exception, and use deductive reasoning and process of elimination to find out where your problem in.
Environment.FastFail(String) 实际上可以是一个很好的调试工具。例如,假设您有一个应用程序,它给您提供了一些奇怪的输出。你不知道为什么。你知道这是错误的,但没有例外冒泡到表面来帮助你。好吧,如果您可以访问 Visual Studio 2005 的 Debug->Exceptions... 菜单项,您实际上可以告诉 Visual Studio 允许您查看那些第一次机会异常。如果你没有,但是你可以把 Environment.FastFail(String) 放在一个异常中,并使用演绎推理和消除过程来找出你的问题所在。
回答by Ed Guiness
回答by PRINCESS FLUFF
It kills the application and even skips try/finally blocks.
它会杀死应用程序,甚至跳过 try/finally 块。
回答by Please treat your mods well.
It also creates a dump and event viewer entry, which might be useful.
它还创建一个转储和事件查看器条目,这可能很有用。
回答by Teoman shipahi
Quote from here: https://msdn.microsoft.com/en-us/library/ms229030(v=vs.110).aspx
从这里引用:https: //msdn.microsoft.com/en-us/library/ms229030(v=vs.110).aspx
? CONSIDER terminating the process by calling System.Environment.FailFast(.NET Framework 2.0 feature) instead of throwing an exception if your code encounters a situation where it is unsafe for further execution.
? 如果您的代码遇到无法安全执行的情况,请考虑通过调用System.Environment.FailFast(.NET Framework 2.0 功能)来终止进程,而不是抛出异常。
回答by Axel Rietschin
Failfast can be used in situations where you might be endangering the user's data. Say in a database engine, when you detect a corruption of your internal data structures, the only sane course of action is to halt the process as quickly as possible, to avoid writing garbage to the database and risk corrupting it and lose the user's data. This is one possible scenario where failfast is useful.
Failfast 可用于可能危及用户数据的情况。假设在数据库引擎中,当您检测到内部数据结构损坏时,唯一明智的做法是尽快停止进程,以避免将垃圾写入数据库并避免损坏数据库和丢失用户数据的风险。这是故障快速有用的一种可能情况。
Another use is to catch programmer errors. Say you are writing a library and some function accepts a pointer that cannot be null in any circumstance, that is, if it's null, you are clearly in presence of a programmer error. You can return an error like E_POINTER or throw some InvalidArgument exception and hope someone notices, but you'll get their attention better by failing fast :-)
另一个用途是捕捉程序员错误。假设您正在编写一个库并且某个函数接受一个在任何情况下都不能为空的指针,也就是说,如果它为空,那么您显然存在程序员错误。您可以返回 E_POINTER 之类的错误或抛出一些 InvalidArgument 异常并希望有人注意到,但通过快速失败,您会更好地引起他们的注意:-)
Note that I'm not restricting the example to pointers, you can generalize to any parameter or condition that should never happen. Failing fast ultimately results in better quality apps, as many bugs no longer go unnoticed.
请注意,我并没有将示例限制为指针,您可以概括为任何不应该发生的参数或条件。快速失败最终会产生更好质量的应用程序,因为许多错误不再被忽视。
Finally, failing fast helps with capturing the state of the process as faithfully as possible (as a memory dump gets created), in particular when failing fast immediatelyupon detecting an unrecoverable error or a really unexpected condition.
最后,快速失败有助于尽可能忠实地捕获进程的状态(因为创建了内存转储),特别是在检测到不可恢复的错误或真正意外的情况后立即快速失败时。
If the process was allowed to continue, say the 'finally' clauses would run or the stack would be unwound, and things would get destroyed or disposed-of, before a memory dump is taken, then the state of the process might be altered in such as way that makes it much more difficult to diagnose the root cause of the problem.
如果允许进程继续,比如说“finally”子句将运行或堆栈将被解开,并且在进行内存转储之前,事情将被销毁或处置,那么进程的状态可能会在例如,这使得诊断问题的根本原因变得更加困难。
回答by Luke Puplett
Joe Duffy discusses failing fast and the discipline to make it useful, here.
Joe Duffy 在这里讨论了快速失败和使其有用的纪律。
http://joeduffyblog.com/2014/10/13/if-youre-going-to-fail-do-it-fast/
http://joeduffyblog.com/2014/10/13/if-youre-going-to-fail-do-it-fast/
Essentially, he's saying that for programming bugs - i.e. unexpected errors that are the fault of the programmer and not the programme user or other inputs or situations that can be reasonable expectedto be bad - then deciding to always fail fast for unexpected errors has been seen to improve code quality.
从本质上讲,他是说对于编程错误——即意外错误是程序员的错,而不是程序用户或其他可以合理预期为坏的输入或情况——然后决定总是因意外错误而快速失败。以提高代码质量。
I think since its an optional team decision and discipline, use of this API in C# is rare since in reality we're all mostly writing LoB apps for 12 people in HR or an online shop at best.
我认为,由于它是可选的团队决策和纪律,因此在 C# 中很少使用此 API,因为实际上我们主要为 HR 或在线商店中的 12 人编写 LoB 应用程序。
So for us, we'd maybe use this when we want deny the consumer of our API the opportunity of making any further moves.
所以对我们来说,当我们想要拒绝我们 API 的消费者采取任何进一步行动的机会时,我们可能会使用它。