程序执行过程中出现的问题是异常,这些异常是对程序运行过程中异常情况的响应,例如当我们尝试除以零并转移控制权时引发的异常通过异常从程序的一个部分到程序的另一部分,异常的处理是通过 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中文网其他相关文章!