Fork me on GitHub

学习C#异常处理机制

异常类型的类别基类 Exception 下存在两类异常:

  • SystemException派生的预定义公共语言运行库异常类。

  • ApplicationException派生的用户定义的应用程序异常类。

Exception 包含很多属性,可以帮助标识异常的代码位置、类型、帮助文件和原因:StackTrace、InnerException、Message、HelpLink、HResult、Source、TargetSite Data。

 


以下我开始对这几个属性开始测试代码演示如下:
Code
 1 using System;
2
3 namespace Uer_Test
4 {
5 class LableOverflowException : Exception
6 {
7 const string OverflowMessage = "此处引发了一个用户自定义溢出异常";
8 #region(此处引用的Exception类方法接口备注)
9 //public Exception(string message, Exception innerException);
10 // 摘要:
11 // 使用指定错误消息和对作为此异常原因的内部异常的引用来
12 // 初始化 System.Exception 类的新实例。
13 // 参数:
14 // message:
15 // 解释异常原因的错误消息。
16 //
17 // innerException:
18 // 导致当前异常的异常;如果未指定内部异常,则是一个 null 引用
19 #endregion
20 public LableOverflowException(string OvererrorMessage, Exception User_Exception)
21 : base(string.Format("{0}-{1}", OverflowMessage, OvererrorMessage), User_Exception)
22 {
23 this.HelpLink = "http://rohelmx.blog.163.com/";
24 this.Source = ".Net异常处理机制.exe";
25 }
26 }
27 class ArrayStringDemo
28 {
29 protected string[] ArrayDemo;
30 protected int Number;
31 public ArrayStringDemo(int index)
32 {
33 ArrayDemo = new String[index];
34 Number = 0;
35 }
36 // 下面的方法数组越界异常被捕获将抛出一个异常
37 public int Addelement(string newelement)
38 {
39 try
40 {
41 ArrayDemo[Number] = newelement;
42 return Number++;
43 }
44 catch(Exception e)
45 {
46 throw new LableOverflowException(string.Format(@"元素'{0}'不能加入该数组!",newelement),e);
47 }
48 }
49 }
50 class TestExceptionDemo
51 {
52 //创建一个ArrayDemo并强制溢出
53 public static void Main()
54 {
55 ArrayStringDemo ASD = new ArrayStringDemo(4);
56 Console.WriteLine(
57 "演示:\n Exception.Message, \n" +
58 " Exception.HelpLink, \n Exception.Source, \n" +
59 " Exception.StackTrace \n Exception." +
60 "TargetSite \n输出信息列表如下:");
61 try
62 {
63 for (int count = 1; ; count++)
64 {
65 ASD.Addelement(string.Format("{0}", count));
66 }
67 }
68 catch (Exception ex)
69 {
70 Console.WriteLine("\nMessage ---\n{0}", ex.Message);
71 Console.WriteLine("\nHelpLink ---\n{0}",ex.HelpLink);
72 Console.WriteLine("\nSource ---\n{0}", ex.Source);
73 Console.WriteLine( "\nStackTrace ---\n{0}", ex.StackTrace);
74 Console.WriteLine("\nTargetSite ---\n{0}", ex.TargetSite);
75 }
76 Console.ReadKey();
77 }
78 }
79 }

try-catch-finally结构官方实例:
简单的解释一下:其中包含了以一个常规catch子句(既不指定异常类型也不指定异常变量名的catch子句)。 一个try语句最多只能有一个只能包含一个catch子句,而且如果存在他必须是最后一个catch子句,他被认为是任何异常类型的匹配项。
 1 // try_catch_finally.cs
2 using System;
3 public class EHClass
4 {
5 static void Main()
6 {
7 try
8 {
9 Console.WriteLine("Executing the try statement.");
10 throw new NullReferenceException();
11 }
12 catch (NullReferenceException e)
13 {
14 Console.WriteLine("{0} Caught exception #1.", e);
15 }
16 catch
17 {
18 Console.WriteLine("Caught exception #2.");
19 }
20 finally
21 {
22 Console.WriteLine("Executing finally block.");
23 }
24 }
25 }
26
27
28 --------------------------------------------------------------------------------
下面我对这段程序的关键部分做一剖析:
 catch (NullReferenceException e)在这段代码中如果不指定异常变量名(此处是e),那么这个程序仅仅是捕获了NullReferenceException异常,并进行了处理,但是它将不会显示异常信息。但是当我们指定了异常变量名(此处是e),这里的演示便是这种情况,这样可以进一步显示异常信息(包括常规的StackTrace、Message、Source、TargetSite ).

下面的代码我演示一下异常的嵌套和跳转处理机制,很简单,一目了然,不在在做说明
Code
 1 using System;
2
3 namespace 演示
4
5 {
6 class Test
7 {
8 static void F()
9 {
10 throw new Exception("捕获内层异常\n");//抛出异常
11 }
12 static void Main(string[] args)
13 {
14
15 try
16 {
17 try
18 {
19 F();
20 goto label;
21 }
22 catch (Exception e)//捕获内层异常
23 {
24 Console.WriteLine(e.Message);
25 }
26 finally
27 {
28 throw new Exception("内部Finally块引发新的异常\n");
29 Console.WriteLine("内层异常处理此处不会进行\n");
30 //此行不执行
31 }
32 }
33 catch (Exception ex)
34 {
35 Console.WriteLine(ex.Message);
36 }
37 finally
38 {
39 Console.WriteLine("异常转移到外层处理完毕!\n");
40 }
41 label:
42 Console.WriteLine("此处goto语句跳出try语句必须在finally块执行完后执行。");
43 Console.ReadKey();
44 }
45 }
46 }
posted @ 2012-03-07 20:19  Halower  阅读(1817)  评论(0编辑  收藏  举报