What is Environment.FailFast?

后端 未结 7 1752
别那么骄傲
别那么骄傲 2021-02-01 14:05

What is Environment.FailFast?

How is it useful?

相关标签:
7条回答
  • 2021-02-01 14:15

    From .NET Framework Design Guidelines on Exception Throwing:

    ✓ 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.

    0 讨论(0)
  • 2021-02-01 14:16

    It kills the application and even skips try/finally blocks.

    0 讨论(0)
  • 2021-02-01 14:21

    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.

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

    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 immediately upon 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.

    0 讨论(0)
  • 2021-02-01 14:23

    Joe Duffy discusses failing fast and the discipline to make it useful, here.

    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 expected to 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.

    So for us, we'd maybe use this when we want deny the consumer of our API the opportunity of making any further moves.

    0 讨论(0)
  • 2021-02-01 14:26

    It's a way to immediately exit your application without throwing an exception.

    Documentation is here.

    Might be useful in some security or data-critical contexts.

    0 讨论(0)
  • 2021-02-01 14:28

    It also creates a dump and event viewer entry, which might be useful.

    0 讨论(0)
提交回复
热议问题