程式執行過程中出現的問題是異常,這些異常是對程式運作過程中異常情況的回應,例如當我們嘗試除以零並轉移控制權時引發的異常透過異常從程式的一個部分到程式的另一部分,異常的處理是透過C# 中的四個關鍵字來管理的,它們是try、catch、finally 和throw 區塊。
C# 中有多種類型的異常。他們是:
由於可用記憶體不足而產生的錯誤由此異常處理。考慮下面的範例程式來示範 System.記憶體不足異常。
範例:
//a class called check is defined public class check { //main method is called public static void Main() { // a string variable is created and tried to store 2.1 billion characters and this causes an out of memory exception string val = new string('r', int.MaxValue); } }
輸出:
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫main方法。創建了一個字串變數並嘗試儲存 21 億個字符,這導致記憶體不足異常。
引用空物件產生的錯誤由此異常處理。考慮下面的範例程式來示範 System. NullReferenceException
範例:
using System; //a class called check is defined class check { //main method is called static void Main() { //a string variable is defined, and it is referencing to null string value = null; //the length of the value referencing to null is checked if it is equal to zero causing an exception if (value.Length == 0) { Console.WriteLine(value); } } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫main方法。然後定義一個字串變量,它引用null。然後檢查引用 null 的值的長度是否等於零,從而導致異常。
類型轉換期間產生的錯誤由此異常處理。考慮下面的範例程式來示範 System. InvalidCastException。
範例:
using System.IO; using System.Text; //a class called check is defined class check { //main method is called static void Main() { // an instance of the string builder class is created which is then assigned to a new object through implicit casting and then casting is tried explicitly to convert the instance of stringbuilder class to streamreader class StringBuilder ref1 = new StringBuilder(); object ref2 = ref1; StreamReader ref3 = (StreamReader)ref2; } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫main方法。然後建立字串產生器類別的實例,然後透過隱含轉換將其指派給新對象,然後明確地嘗試轉換以將 stringbuilder 類別的實例轉換為 Streamreader 類,這會導致異常。
當類型與陣列類型不符時產生的錯誤由此異常處理。考慮下面的範例程式來示範 System. ArrayTypeMismatchException。
範例:
//a class called check is defined class check { //main method is called static void Main() { // a string is defined and assigned the values which is then assigned to object class array and then an integer is tried to put in the same array which causes an exception string[] arr1 = { "Welcome", "to", "CSharp" }; object[] arr2 = arr1; arr2[0] = 8; } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後定義main方法。然後定義一個字串並分配值,然後將其分配給物件類別數組,然後嘗試將整數放入同一數組中,這會導致異常。
當方法引用超出範圍的陣列時產生的錯誤由此異常處理。考慮下面的範例程式來示範 System. IndexOutOfRangeException。
範例:
//a class called check is defined class check { //main method is called static void Main() { // an array is defined to store 100 integers but then an integer is tried to be stores at a position outside of the size of the array which causes an exception int[] arr = new int[10]; arr[0] = 10; arr[10] = 20; arr[20] = 30; } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫main方法。然後定義一個陣列來儲存 100 個整數,但隨後嘗試將一個整數儲存在超出陣列大小的位置,這會導致異常。
被除數除以零時產生的錯誤由此異常處理。考慮下面的範例程式來示範 System.除以零異常。
範例:
using System; //a class called check is defined class check { //main method is called static void Main() { //an integer variable res is defined which is tried to divide by zero which causes an exception int res = 10 / int.Parse("0"); Console.WriteLine(res); } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫main方法。然後定義一個整數變數 res,嘗試除以零,這會導致異常。
堆疊溢位產生的錯誤由此異常處理。考慮下面的範例程式來示範 System. StackOverflowException。
範例:
using System; //a class called check is defined public class check { // a method called recurse is defined which takes a value as parameter and increases its value by one static void Recurse(int val) { // since we have written a recursive loop and 0 is passed as a parameter, it ends in an infinite loop causing exception Console.WriteLine(val); Recurse(++val); } //main method is called public static void Main() { //The recurse method is called to start the infinite recursion Recurse(0); } } <strong>Output:</strong>
In the above program, a class called check is defined. Then a method called recurse is defined which takes a value as a parameter and increases its value by one. Then the main method is called in which the infinite loop for recursion begins by passing zero as a parameter. Then since we have written a recursive loop and 0 is passed as a parameter, it ends in an infinite loop causing an exception.
The errors that are generated by input, the output is handled by this exception. Consider the below example program to demonstrate System. IO. IOException.
Example:
using System; using System.IO; //a class called check is defined class check { //main methos is called static void Main() { try { //a file is tried to open which do not exist and causes an exception File.Open("D:\\ex.txt", FileMode.Open); } catch (IOException) { Console.WriteLine("Inputoutput Exception is handled"); } } }
Output:
In the above program, a class called check is defined. Then the main method is called. Then a file is tried to open which does not exist and causes an exception.
以上是C# 中的異常類型的詳細內容。更多資訊請關注PHP中文網其他相關文章!