Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Demonstrate the Use of FailFast() Method of Environment Class

  • Last Updated : 01 Nov, 2021

Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the call stack information, and time since last system boot in milliseconds information. In this article, we will discuss the FailFast() method of the Environment class. This method can be overloaded in two different ways:

1. FailFast(string msg): This method is used to terminate the process immediately after writing the Windows application event log and adds the message in the error report to Microsoft. It terminates the process without running the finalizers or try/catch block. 

Syntax:

public static void FailFast(string msg)

Here, msg is a message that will be logged in the windows application event log or it explains why the termination happens. It can be null when no explanation is given.



2. FailFast(string msg, Exception): This method is used to terminate the process immediately after writing the Windows application event log and adds the message and exceptions in the error report to Microsoft. Here the exception does not handle because the process is terminated but can get the information due to which exception happens. In this method, when the exception is null then FailFast(string msg, Exception) method behaves like FailFast(string msg) method. It also terminates the process without running the finalizers or try/catch block. 

Syntax:

public static void FailFast(string msg, Exception)

Here, msg is a message that will be logged in the windows application event log or it explains why the termination happens and the exception is an error due to which the termination happens.

Let us discuss the above concept with the help of the examples:

Example 1:

C#




// C# program to illustrate how to use FailFast() method 
// of Environment Class
using System;
  
class GFG{
      
static public void Main()
{
      
    // Code before termination
    Console.WriteLine("Before termination");
      
    // Usage of FailFast() method to
    // terminate the code
    Environment.FailFast("Stop the program");
      
    // Code after termination
    Console.WriteLine("After termination");
}
}

Output:

Before termination
CLR: Managed code called FailFast, saying "Stop the program"

=================================================================
    Native Crash Reporting
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Example 2:

In this example, we perform arithmetic operations before and after termination.

C#




// C# program to illustrate how to use FailFast() method 
// of Environment Class
using System;
  
class GFG{
      
static public void Main()
{
      
    // Add two numbers
    int Sum = 10 + 20;
    Console.WriteLine("Sum:" + Sum);
      
    // Code before termination
    Console.WriteLine("Before termination");
      
    // Here, we use of FailFast() method to
    // terminate the code
    Environment.FailFast("Stop the program");
      
    // Code after termination
    Console.WriteLine("After termination");
      
    // Subtract two numbers
    int Subt = 20-10;
    Console.WriteLine("Subtraction: " + Subt);
}
}

Output:

Sum:30
Before termination
CLR: Managed code called FailFast, saying "Stop the program"

=================================================================
    Native Crash Reporting
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!