首頁  >  文章  >  後端開發  >  C# 中的自訂異常

C# 中的自訂異常

王林
王林原創
2024-09-03 15:21:32988瀏覽

在我們正在開發的應用程式中,當發生違反業務規則的情況時,我們會引發自訂異常來處理這種情況。自訂異常是專門為解決應用程式中的獨特場景而創建的異常。為了建立自訂異常,我們從 C# 中的 ApplicationException 或 Exception 類別衍生出一個新類別。 ApplicationException 類別從 .NET v1.0 版本開始包含在 .NET Framework 中,旨在用作 C# 中自訂例外類別的基底類別。

C# 中自訂異常的工作

  • 異常處理應用程式執行期間​​發生的錯誤類型。錯誤是指應用程式執行過程中出現的意外問題。相反,由於多種原因,在應用程式執行期間​​預計會發生異常。
  • 應用程式使用異常處理來處理應用程式執行期間​​預期發生的異常。 C# 中異常的處理是透過 try、catch、finally 和 throw 這兩個關鍵字來完成的。
  • 類別代表 C# 中的異常。 C# 中的這些異常類別衍生自 System.異常類別可以直接方式或間接方式。
  • Application Exception 類別或 Exception 類別用於建立可以拋出的自訂例外。
  • 只有當這些異常可以被捕獲並可以以不同的方式處理時,創建自訂異常才有用。
  • 應用程式發生錯誤,如果我們在C#中建立自訂異常,可以使用錯誤監控工具監控錯誤日誌。

範例

下面給出的是提到的例子:

範例#1

C# 程序,示範在程式中使用自訂異常。

代碼:

using System;
//a namespace called user defined is defined
namespace UserDefined
{
//a class called test weather is defined
class Testweather
{
//main method is called
static void Main(string[] args)
{
//an instance of the class temperat is defined
Temperat tem = new Temperat();
try
{
//the show method of temperat class is called using the instance of the temperat class
tem.show();
}
catch(WeatheriscoldException e)
{
Console.WriteLine("The weather is cold Exception: {0}", e.Message);
}
Console.ReadKey();
}
}
}
//a custom exception class called Weather is cold Exception class is created which is thrown if the weather is cold
public class WeatheriscoldException: Exception
{
public WeatheriscoldException(string message): base(message)
{
}
}
//a class called temperat is defined
public class Temperat
{
//a variable called temp is defined and assigned to zero
int temp = 0;
//a method called show is defined
public void show()
{
//the temperature is checked to determine the weather
if(temp == 0)
{
throw (new WeatheriscoldException("The temperature is found to be zero and hence the weather is cold"));
}
else
{
Console.WriteLine("The Temperature is: {0}", temp);
}
}
}

輸出:

C# 中的自訂異常

說明:

  • 在上面的程式中,定義了一個名為user-define的命名空間。然後定義一個名為測試天氣的類別。然後呼叫main方法。然後定義類別 tempat 的實例。然後使用 tempat 類別的實例呼叫 tempat 類別的 show 方法。
  • 然後建立一個名為 Weather is Cold Exception 的自訂異常類,如果天氣冷則拋出該異常類。然後定義一個名為temperate的類別。然後定義一個名為 temp 的變數並將其賦值為零。然後定義了一個名為show的方法。然後檢查溫度以確定天氣。

範例#2

C# 程序,示範在程式中使用自訂異常。

代碼:

using System;
//a namespace called exception handling is defined
namespace ExceptionHandling
{
//The custom exception class called odd num exception class is created by inheriting the exception class
public class OddNumException : Exception
{
//The property message is being overridden here
public override string Message
{
get
{
return "There cannot be an odd divisor";
}
}
}
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//three integer variables are defined
int a, b, c;
Console.WriteLine("Please enter two numbers and type of the numbers must be integer:");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
try
{
//checking if the divisor is an odd number or an even number
if (b % 2 > 0)
{
//exception is thrown if the divisor is an odd number
throw new OddNumException();
}
c = a / b;
Console.WriteLine(c);
}
catch (OddNumException two)
{
Console.WriteLine(two.Message);
}
Console.WriteLine("The program ends here");
Console.ReadKey();
}
}
}

輸出:

C# 中的自訂異常

說明:

  • 在上面的程式中,定義了一個名為異常處理的命名空間。那麼這個類稱為奇數異常類,就是透過繼承異常類別所創建的。然後屬性訊息在那裡被覆蓋。然後定義一個名為check的類別。然後呼叫main方法。然後定義三個整數變數來接受兩個輸入整數變量,另一個整數變數用於儲存整數輸出。
  • 然後使用 parse() 方法解析兩個整數變數。然後檢查第二個整數變數或除數,看看它是奇數還是偶數,這是透過檢查除數除以二的餘數是否大於零或等於零來完成的。如果除數是奇數,則拋出異常。

優點

以下是提到的優點:

  • C#中自訂異常類型的自訂處理可以透過呼叫程式碼來完成。
  • 可以透過使用 C# 中的自訂異常處理來自訂圍繞自訂異常類型的監控。

以上是C# 中的自訂異常的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn