📜  C#程序演示环境类FailFast()方法的使用

📅  最后修改于: 2022-05-13 01:55:41.045000             🧑  作者: Mango

C#程序演示环境类FailFast()方法的使用

环境类提供有关当前平台的信息并操作当前平台。它对于获取和设置各种与操作系统相关的信息很有用。我们可以使用它来检索命令行参数信息、退出代码信息、环境变量设置信息、调用堆栈信息的内容以及自上次系统启动以来的时间(以毫秒为单位)信息。在本文中,我们将讨论 Environment 类的 FailFast() 方法。此方法可以通过两种不同的方式重载:

1. FailFast(字符串 msg):该方法用于在写入Windows应用程序事件日志后立即终止进程,并将错误报告中的消息添加到微软。它在不运行终结器或 try/catch 块的情况下终止进程。

句法:

在这里, msg是一条将记录在 Windows 应用程序事件日志中的消息,或者它解释了终止发生的原因。不解释时可以为空。

2. FailFast(字符串 msg, Exception):该方法用于在写入Windows应用程序事件日志后立即终止进程,并将错误报告中的消息和异常添加到微软。此处异常不处理,因为进程已终止,但可以获取发生异常的信息。在此方法中,当异常为 null 时,FailFast(字符串 msg, Exception) 方法的行为类似于 FailFast(字符串 msg) 方法。它还会在不运行终结器或 try/catch 块的情况下终止进程。

句法:

在这里, msg是一条将记录在 Windows 应用程序事件日志中的消息,或者它解释了终止发生的原因,而异常是导致终止发生的错误。

让我们借助示例讨论上述概念:

示例 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");
}
}


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);
}
}


输出:

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

示例 2:

在此示例中,我们在终止之前和之后执行算术运算。

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);
}
}

输出:

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