目录
C# 中的异常类型及示例
1. System.OutOfMemoryException
2. System.NullReferenceException
3. System.InvalidCastException
4. System.ArrayTypeMismatchException
5. System.IndexOutOfRangeException
6. System.DivideByZeroException
7. System.StackOverflowException
8. System.IO.IOException
首页 后端开发 C#.Net教程 C# 中的异常类型

C# 中的异常类型

Sep 03, 2024 pm 03:20 PM
c# c# tutorial

程序执行过程中出现的问题是异常,这些异常是对程序运行过程中异常情况的响应,例如当我们尝试除以零并转移控制权时引发的异常通过异常从程序的一个部分到程序的另一部分,异常的处理是通过 C# 中的四个关键字来管理的,它们是 try、catch、finally 和 throw 块。

C# 中的异常类型及示例

C# 中有多种类型的异常。他们是:

1. System.OutOfMemoryException

由于可用内存不足而产生的错误由该异常处理。考虑下面的示例程序来演示 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);
}
}
登录后复制

输出:

C# 中的异常类型

输出:

在上面的程序中,定义了一个名为check的类。然后调用main方法。创建了一个字符串变量并尝试存储 21 亿个字符,这导致内存不足异常。

2. System.NullReferenceException

引用空对象产生的错误由此异常处理。考虑下面的示例程序来演示 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);
}
}
}
登录后复制

输出:

C# 中的异常类型

在上面的程序中,定义了一个名为check的类。然后调用main方法。然后定义一个字符串变量,它引用null。然后检查引用 null 的值的长度是否等于零,从而导致异常。

3. System.InvalidCastException

类型转换期间生成的错误由此异常处理。考虑下面的示例程序来演示 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;
}
}
登录后复制

输出:

C# 中的异常类型

在上面的程序中,定义了一个名为check的类。然后调用main方法。然后创建字符串生成器类的实例,然后通过隐式转换将其分配给新对象,然后显式尝试转换以将 stringbuilder 类的实例转换为 Streamreader 类,这会导致异常。

4. System.ArrayTypeMismatchException

当类型与数组类型不匹配时产生的错误由此异常处理。考虑下面的示例程序来演示 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;
}
}
登录后复制

输出:

C# 中的异常类型

在上面的程序中,定义了一个名为check的类。然后定义main方法。然后定义一个字符串并分配值,然后将其分配给对象类数组,然后尝试将整数放入同一数组中,这会导致异常。

5. System.IndexOutOfRangeException

当方法引用超出范围的数组时生成的错误由此异常处理。考虑下面的示例程序来演示 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;
}
}
登录后复制

输出:

C# 中的异常类型

在上面的程序中,定义了一个名为check的类。然后调用main方法。然后定义一个数组来存储 100 个整数,但随后尝试将一个整数存储在数组大小之外的位置,这会导致异常。

6. System.DivideByZeroException

被除数除以零时产生的错误由此异常处理。考虑下面的示例程序来演示 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);
}
}
登录后复制

输出:

C# 中的异常类型

在上面的程序中,定义了一个名为check的类。然后调用main方法。然后定义一个整数变量 res,尝试除以零,这会导致异常。

7. System.StackOverflowException

堆栈溢出产生的错误由该异常处理。考虑下面的示例程序来演示 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>
登录后复制

C# 中的异常类型

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.

8. System.IO.IOException

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:

C# 中的异常类型

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1668
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
使用 C# 的活动目录 使用 C# 的活动目录 Sep 03, 2024 pm 03:33 PM

使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。

C# 中的随机数生成器 C# 中的随机数生成器 Sep 03, 2024 pm 03:34 PM

C# 随机数生成器指南。在这里,我们讨论随机数生成器的工作原理、伪随机数和安全数的概念。

C# 数据网格视图 C# 数据网格视图 Sep 03, 2024 pm 03:32 PM

C# 数据网格视图指南。在这里,我们讨论如何从 SQL 数据库或 Excel 文件加载和导出数据网格视图的示例。

C# 中的阶乘 C# 中的阶乘 Sep 03, 2024 pm 03:34 PM

C# 阶乘指南。这里我们讨论 C# 中阶乘的介绍以及不同的示例和代码实现。

c#多线程和异步的区别 c#多线程和异步的区别 Apr 03, 2025 pm 02:57 PM

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在这里,我们讨论 C# 中模式的介绍和前 3 种类型,以及其示例和代码实现。

C# 中的质数 C# 中的质数 Sep 03, 2024 pm 03:35 PM

C# 素数指南。这里我们讨论c#中素数的介绍和示例以及代码实现。

xml怎么改格式 xml怎么改格式 Apr 03, 2025 am 08:42 AM

可以采用多种方法修改 XML 格式:使用文本编辑器(如 Notepad )进行手工编辑;使用在线或桌面 XML 格式化工具(如 XMLbeautifier)进行自动格式化;使用 XML 转换工具(如 XSLT)定义转换规则;或者使用编程语言(如 Python)进行解析和操作。修改时需谨慎,并备份原始文件。

See all articles