Dalam aplikasi yang sedang kami usahakan, apabila pelanggaran peraturan perniagaan berlaku, kami menimbulkan pengecualian tersuai untuk menangani situasi tersebut. Pengecualian tersuai ialah pengecualian yang dibuat khusus untuk menangani senario unik dalam aplikasi kami. Untuk mencipta pengecualian tersuai, kami memperoleh kelas baharu daripada kelas ApplicationException atau Exception dalam C#. Kelas ApplicationException disertakan dalam Rangka Kerja .NET bermula dari versi .NET v1.0 dan direka bentuk untuk berfungsi sebagai kelas asas untuk kelas pengecualian tersuai dalam C#.
Diberikan di bawah adalah contoh yang dinyatakan:
Atur cara C# untuk menunjukkan penggunaan Pengecualian Tersuai dalam atur cara.
Kod:
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); } } }
Output:
Penjelasan:
Atur cara C# untuk menunjukkan penggunaan Pengecualian tersuai dalam atur cara.
Kod:
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(); } } }
Output:
Penjelasan:
Diberikan di bawah adalah kelebihan yang dinyatakan:
Atas ialah kandungan terperinci Pengecualian Tersuai dalam C#. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!