当前位置:网站首页>C#入门系列(三十) -- 异常处理
C#入门系列(三十) -- 异常处理
2022-07-25 08:07:00 【InfoQ】
概念
trycatchfinallythrow- try:一个 try 块标识了一个将被激活的特定的异常的代码块。后跟一个或多个 catch 块
- catch:程序通过异常处理程序捕获异常。catch 关键字表示异常的捕获
- finally:finally 块用于执行给定的语句,不管异常是否被抛出都会执行。例如,如果您打开一个文件,不管是否出现异常文件都要被关闭
- throw:当问题出现时,程序抛出一个异常。使用 throw 关键字来完成。
三种异常的处理语句
- try ... catch //捕获异常
- try ... finally //清除异常
- try ... catch ... finally // 处理所有异常
try
{
//包含容易产生异常的代码
}
catct(异常类 ,异常实例对象)
{
//异常处理代码
}
namespace 捕获异常
{
class Program
{
static void Main(string[] args)
{
//利用 try catch 语句捕获数组越界问题
int[] arr = { 0, 1, 2, 3, 1, 2, 3, 3, 4, 5 };
try
{
for (int i = 0; i <= arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
}
catch(Exception myex)
{
//Message.Tostring()中的Message是获取描述当前异常消息
Console.WriteLine(myex.Message.ToString());
}
//此语句虽然可以给出异常提示,并不能智能描述异常的原因
catch
{
Console.WriteLine("发生异常");
}
Console.ReadKey();
}
}
}
try
{
// 包含容易产生异常的代码
}
finally
{
// 用于消除try 块中分配的任何资源以及
// 运行任何即使在发生异常时也必须执行的代码
}
try
{
// 包含容易产生异常的代码
}
catct(异常类 ,异常实例对象)
{
// 异常处理代码
}
finally
{
// 用于消除try 块中分配的任何资源以及
// 运行任何即使在发生异常时也必须执行的代码
}
预定义异常类
- Exception:所有异常对象的基类
- SystemException:运行时产生的所有错误的基类
- IdexOutOfRangeExceptiom:当数组的下标超出范围时运行时引发
- NullRefereceException:当一个空对象被引用时运行时引发
- ArgunmentException:所有参数异常的基类
- InvalidCastException:类型的显示转换在运行是失败时,就会引发异常
- ArrayTypeMismathException:当存储一个数组是,如果由于被存储的元素实际类型与数组的实际类型不兼容而导致存储失败就会引发此异常
- ArithmeticException:算术运算期间异常发生的基类
- DivideByZeroException:试图除以0引发
- OverflowException:溢出时引发
- FormatException:参数格式无效是引发
边栏推荐
- Raspberrypico serial communication
- Numpy learning
- Oracle trigger creation
- People who lose weight should cry: it's no good not eating food, because your brain will be inflamed
- Machine learning theory and case analysis (Part2) -- Regression
- app耗电量测试
- A review of nature: gender differences in anxiety and depression - circuits and mechanisms
- "Unable to recognize" yarn "item as cmdlet, function, script file
- Dijkstra序列(暑假每日一题 5)
- Niuke dynamic planning training
猜你喜欢

Brush the title "sword finger offer" day01

enq: HW – contention等待引起的故障分析
Mysql-5.7 installation

Eval and assert one sentence Trojan horse analysis

The difference between batchnorm and layernorm

Today in history: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal

【着色器实现Shadow投影效果_Shader效果第八篇】

Competition path design of beacon group

JS cool rolling picture deformation animation JS special effects

【音视频】图片YUV数据格式
随机推荐
Node+js build time server
The difference between batchnorm and layernorm
Technical Analysis | Doris connector combined with Flink CDC to achieve accurate access to MySQL database and table exactly once
P1049 [noip2001 popularization group t4] packing problem
yolov7 网络架构深度解析
redis客户端工具redis-insight推荐
Codeforces Round #809 Editorial(A,B,C)
CAS operation
牛客动态规划训练
Test the mock data method of knowing and knowing
设计一个有getMin功能的栈
Mysql-5.7 installation
【着色器实现Shadow投影效果_Shader效果第八篇】
文献学习(part101)--CONVEX BICLUSTERING
The 17th revitalization cup computer programmer (cloud computing platform operation and development) finals
第3章业务功能开发(实现全选按钮实时的响应)
Open source, innovators win, 2022 "science and innovation China" open source innovation list selection is fully open!
P1048 [noip2005 popularization group t3] drug collection
IoT物联网嵌入式设备中30种常见传感器模块简介及原理讲解
CAS操作