异常处理是C# 语言的一个重要特性。异常处理用于捕获程序中发生的异常。
在异常处理中,它提供了以下三个功能
- try 代码块
- catch 代码块
- finally 代码块
为了处理这些异常,c#为开发人员提供System.exception命名空间。
它提供了处理异常所需的基本类和方法。
异常类的属性有:
- Data
- HelpLink
- HResult
- InnerException
- Message
- Source
- TargetSite
C# System.Exception 命名空间示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Vipendra { static void show(string a) { if (a == null) { throw new ArgumentNullException(); } } static void Main(string[] args) { try { string a = null; show(a); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); Console.ReadLine(); } finally { Console.WriteLine("执行 finally 块."); Console.ReadLine(); } } } }
日期:2020-04-11 23:03:53 来源:oir作者:oir