The FailFast method stops When an application or program becomes critically unstable, it is sometimes better to exit, rather than risk corruption. If even exiting normally could cause damage to data, it is appropriate to ignore any exception handling and stop the process immediately.
Environment.FailFast
The FailFast method stops your program immediately, ensuring that no further code can execute. As this means that you cannot report the problem directly to the user, this static method includes a parameter, to which you can provide a string containing a status message. This message is recorded in the application event log.
To demonstrate, create a new console application project and add the following code to the Main method. The code includes commands to output four messages to the console. The first is displayed before the call to Environment.FailFast. As FailFast exits the program straight away, the following messages within the try, catch and finally blocks are not shown.
try
{
Console.WriteLine("Start");
Environment.FailFast("Goodbye, I don't want to live in this world!");
Console.WriteLine("End");
}
catch
{
Console.WriteLine("Catch Block");
}
finally
{
Console.WriteLine("Finally Block");
}
Try running the code without debugging be pressing Ctrl-F5. You'll see just the "Start" message before the program crashes. If your check the application log you'll see an error including the text from FailFast call's argument. Also try running withing the Visual Studio debugger by pressing F5. When the call to FailFast is encountered the program will stop and display a FatalExecutionEngineError. This allows you to see where the failure happened. No further code will execute.
No comments:
Post a Comment