当前位置: 首页 > news >正文

windows C#-异常处理

C# 程序员使用 try 块来对可能受异常影响的代码进行分区。 关联的 catch 块用于处理生成的任何异常。 finally 块包含无论 try 块中是否引发异常都会运行的代码,如发布 try 块中分配的资源。 try 块需要一个或多个关联的 catch 块或一个 finally 块,或两者皆之。

下面的示例演示 try-catch 语句、try-finally 语句和 try-catch-finally 语句。

try
{// Code to try goes here.
}
catch (SomeSpecificException ex)
{// Code to handle the exception goes here.// Only catch exceptions that you know how to handle.// Never catch base class System.Exception without// rethrowing it at the end of the catch block.
}try
{// Code to try goes here.
}
finally
{// Code to execute after the try block goes here.
}try
{// Code to try goes here.
}
catch (SomeSpecificException ex)
{// Code to handle the exception goes here.
}
finally
{// Code to execute after the try (and possibly catch) blocks// goes here.
}

一个不具有 catch 或 finally 块的 try 块会导致编译器错误。

catch 块

catch 块可以指定要捕获的异常的类型。 该类型规范称为异常筛选器。 异常类型应派生自 Exception。 一般情况下,不要将 Exception 指定为异常筛选器,除非了解如何处理可能在 try 块中引发的所有异常,或者已在 catch 块的末尾处包括了 throw 语句。

可将具有不同异常类的多个 catch 块链接在一起。 代码中 catch 块的计算顺序为从上到下,但针对引发的每个异常,仅执行一个 catch 块。 将执行指定所引发的异常的确切类型或基类的第一个 catch 块。 如果没有 catch 块指定匹配的异常类,则将选择不具有类型的 catch 块(如果语句中存在)。 务必首先定位具有最具体的(即,最底层派生的)异常类的 catch 块。

当以下条件为 true 时,捕获异常:

1. 能够很好地理解可能会引发异常的原因,并且可以实现特定的恢复,例如捕获 FileNotFoundException 对象时提示用户输入新文件名。
2. 可以创建和引发一个新的、更具体的异常。

int GetInt(int[] array, int index)
{try{return array[index];}catch (IndexOutOfRangeException e){throw new ArgumentOutOfRangeException("Parameter index is out of range.", e);}
}

3. 想要先对异常进行部分处理,然后再将其传递以进行更多处理。 在下面的示例中,catch 块用于在重新引发异常之前将条目添加到错误日志。 

try
{// Try to access a resource.
}
catch (UnauthorizedAccessException e)
{// Call a custom error logging procedure.LogError(e);// Re-throw the error.throw;
}

还可以指定异常筛选器,以向 catch 子句添加布尔表达式。 异常筛选器表明仅当条件为 true 时,特定 catch 子句才匹配。 在以下示例中,两个 catch 子句均使用相同的异常类,但是会检查其他条件以创建不同的错误消息:

int GetInt(int[] array, int index)
{try{return array[index];}catch (IndexOutOfRangeException e) when (index < 0) {throw new ArgumentOutOfRangeException("Parameter index cannot be negative.", e);}catch (IndexOutOfRangeException e){throw new ArgumentOutOfRangeException("Parameter index cannot be greater than the array size.", e);}
}

始终返回 false 的异常筛选器可用于检查所有异常,但不可用于处理异常。 典型用途是记录异常:

public class ExceptionFilter
{public static void Main(){try{string? s = null;Console.WriteLine(s.Length);}catch (Exception e) when (LogException(e)){}Console.WriteLine("Exception must have been handled");}private static bool LogException(Exception e){Console.WriteLine($"\tIn the log routine. Caught {e.GetType()}");Console.WriteLine($"\tMessage: {e.Message}");return false;}
}

LogException 方法始终返回 false,使用此异常筛选器的 catch 子句均不匹配。 catch 子句可以是通用的,使用 System.Exception后面的子句可以处理更具体的异常类。

Finally 块

finally 块让你可以清理在 try 块中所执行的操作。 如果存在 finally 块,将在执行 try 块和任何匹配的 catch 块之后,最后执行它。 无论是否会引发异常或找到匹配异常类型的 catch 块,finally 块都将始终运行。

finally 块可用于发布资源(如文件流、数据库连接和图形句柄)而无需等待运行时中的垃圾回收器来完成对象。

在下面的示例中,finally 块用于关闭在 try 块中打开的文件。 请注意,在关闭文件之前,将检查文件句柄的状态。 如果 try 块不能打开文件,则文件句柄仍将具有值 null 且 finally 块不会尝试将其关闭。 或者,如果在 try 块中成功打开文件,则 finally 块将关闭打开的文件。

FileStream? file = null;
FileInfo fileinfo = new System.IO.FileInfo("./file.txt");
try
{file = fileinfo.OpenWrite();file.WriteByte(0xF);
}
finally
{// Check for null because OpenWrite might have failed.file?.Close();
}
http://www.lryc.cn/news/481488.html

相关文章:

  • 边缘计算在智能制造中的应用
  • 点云开发:从入门到精通的全面教程
  • 【含文档】基于ssm+jsp的商店会员系统(含源码+数据库+lw)
  • 【大数据学习 | kafka高级部分】文件清除原理
  • dolphin 配置data 从文件导入hive 实践(一)
  • Docker Compose部署Rabbitmq(脚本下载延迟插件)
  • 麦当劳自助点餐机——实现
  • C++ STL CookBook 6:STL Containers (I)
  • 行转列实现方式总结
  • 【go从零单排】初探goroutine
  • HarmonyOS NEXT应用元服务开发Intents Kit(意图框架服务)本地搜索接入方案
  • C语言可变参数列表编程实战指南:从基础概念到高级应用的全面解析
  • AndroidStudio-文本显示
  • HBuilderX运行微信小程序,编译的文件在哪,怎么运行
  • 百亿AI数字人社会初现:Project Sid展示智能代理文明进化路径
  • 代码随想录训练营Day21 | 491.递增子序列 - 46.全排列 - 47.全排列 II - 332.重新安排行程 - 51.N皇后 - 37.解数独
  • 多用户商城系统的功能及设计和开发
  • 2024年11月8日day8
  • Debezium系列之:Debezium3版本增量快照和只读增量快照应用的变化
  • Python正则表达式1 re.match惰性匹配详解案例
  • WPF(C#)学习日志10:Prism框架下按键绑定
  • WPF中的ResizeMode
  • Unity3D UI 双击和长按
  • LabVIEW扫描探针显微镜系统
  • 问题式教学法在生物教学中的应用探索
  • C++ | Leetcode C++题解之第556题下一个更大元素III
  • 实现链式结构二叉树
  • 在vscode中如何利用git 查看某一个文件的提交记录
  • 【ShuQiHere】️`adb kill-server` 和 `adb start-server` 命令的作用
  • 植物明星大乱斗1