環境類提供有關當前平台的信息並操作當前平台。它對於獲取和設置各種操作 system-related 信息很有用。我們可以使用它來檢索 命令行 參數信息、退出代碼信息、環境變量設置信息、調用堆棧信息的內容以及自上次係統啟動以來的時間(以毫秒為單位)信息。在本文中,我們將討論 Environment 類的 FailFast() 方法。此方法可以通過兩種不同的方式重載:
1. FailFast(string msg):該方法用於在寫入Windows應用程序事件日誌後立即終止進程,並將錯誤報告中的消息添加到微軟。它在不運行終結器或 try/catch 塊的情況下終止進程。
用法:
public static void FailFast(string msg)
在這裏,msg 是一條將記錄在 Windows 應用程序事件日誌中的消息,或者它解釋了終止發生的原因。不解釋時可以為空。
2. FailFast(string msg, Exception):該方法用於在寫入Windows應用程序事件日誌後立即終止進程,並將錯誤報告中的消息和異常添加到微軟。此處異常不處理,因為進程已終止,但可以獲取發生異常的信息。在此方法中,當異常為 null 時,FailFast(string msg, Exception) 方法的行為類似於 FailFast(string msg) 方法。它還會在不運行終結器或 try/catch 塊的情況下終止進程。
用法:
public static void FailFast(string msg, Exception)
在這裏,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");
}
}
輸出:
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. =================================================================
相關用法
注:本文由純淨天空篩選整理自manojkumarreddymallidi大神的英文原創作品 C# Program to Demonstrate the Use of FailFast() Method of Environment Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。