C# Exception异常 学习

@1:try - catch

Exception类几个常用属性的示例:   TargetSite,  StackTrace,  Source

示例1
 1 namespace _20130405
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Loop:
 8             try
 9             {                
10                 Console.WriteLine("Input Intager One:");
11                 int x = Convert.ToInt32(Console.ReadLine());
12                 Console.WriteLine("Input Intager Two:");
13                 int y = Convert.ToInt32(Console.ReadLine());
14                 Console.WriteLine("x / y = {0}", x / y);
15             }
16             catch (FormatException format)
17             {
18                 //Console.WriteLine(format.Message);
19                 Console.WriteLine(format.TargetSite);       //捕获引发当前异常的方法
20                 Console.WriteLine();
21                 Console.WriteLine();
22                 Console.WriteLine(format.StackTrace);       //捕获当前异常发生所经历的方法的名称和签名
23                 Console.WriteLine();
24                 Console.WriteLine();
25                 Console.WriteLine(format.Source);           //捕获或设置导致错误的应用程序或对象的名称
26                 Console.WriteLine();
27                 Console.WriteLine();
28                 /*
29                 Input Intager One:
30                 1.0
31                 Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuff
32                 er ByRef, System.Globalization.NumberFormatInfo, Boolean)
33 
34 
35                    在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffe
36                 r& number, NumberFormatInfo info, Boolean parseDecimal)
37                    在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo in
38                 fo)
39                    在 System.Convert.ToInt32(String value)
40                    在 _20130405.Program.Main(String[] args) 位置 F:\2013\20130405\20130405\Progr
41                 am.cs:行号 17
42 
43 
44                 mscorlib
45                  */
46             }
47             catch (DivideByZeroException zero)
48             {
49                 //Console.WriteLine(zero.Message);
50                 Console.WriteLine(zero.TargetSite);
51                 Console.WriteLine();
52                 Console.WriteLine();
53                 Console.WriteLine(zero.StackTrace);
54                 Console.WriteLine();
55                 Console.WriteLine();
56                 Console.WriteLine(zero.Source);
57                 Console.WriteLine();
58                 Console.WriteLine();
59                 /*                                 
60                 Input Intager One:
61                 1
62                 Input Intager Two:
63                 0
64                 Void Main(System.String[])
65 
66 
67                    在 _20130405.Program.Main(String[] args)
68 
69 
70                 20130405
71                  */
72             }           
73             catch (Exception e)
74             {
75                 //当捕获多个异常时,若两个catch块的异常类存在继承关系,则要先捕获派生类的异常,再捕获基类的异常.
76                 //否则,捕获派生类异常的catch块将不起作用,并且会在编译时报错. 所以此处的这个catch必须放在三个
77                 //catch中的最后
78                 Console.WriteLine(e.Message);
79             }
80             
81             goto Loop;
82         }
83     }
84 }

@2:try - finally

  在执行时,若没有发生异常,try - finally语句将按正常方式执行,若try块内存在异常,则将在执行完finally块后,抛出异常.

  finally块用于清除try块中分配的任何资源,以及执行  即使发生异常也必须执行的  代码.

@3:try - catch - finally

  在异常处理中最多只能有一个finally块!

示例2
 1 static void Main(string[] args)
 2         {
 3             int sum = 0;
 4             try
 5             {
 6                 int[] a = {1, 3, 4, 5, 6};
 7                 for (int i = 0; i < 6; ++i)
 8                 {
 9                     sum += a[i];
10                 }
11                 Console.WriteLine("try中的:sum = {0}", sum);    //这条语句没有执行
12                 //如果try块中出现了异常,则程序终止try块的语句,进入catch块, 所以此处没有进行输出.
13             }
14             catch (ArgumentOutOfRangeException argu)
15             {
16                 Console.WriteLine(argu.Message);
17             }
18             catch (Exception e)
19             {
20                 Console.WriteLine(e.Message);
21             }
22             finally
23             {
24                 Console.WriteLine("finally中的:sum = {0}", sum);
25             }
26             Console.ReadLine();
27             /*运行结果:
28             索引超出了数组界限。
29             finally中的:sum = 19
30              */
31         }

@4:throw语句用于发出  在程序执行期间出现异常情况  的 信号.

  throw  表达式;

  其中表达式必须表示一个Exception类或它的派生类型,也可以在throw语句后没有表达式,表示将异常再次抛出.

示例3
 1 static void Main(string[] args)
 2         {
 3             int i = 0;
 4             int count = 3;
 5             Console.WriteLine("Please Input the KeyWord:");
 6 
 7             try
 8             {
 9                 while (Console.ReadLine() != "wxy")
10                 {
11                     Console.WriteLine("Wrong KeyWord!");
12 
13                     i++;
14                     if (i >= count)
15                         throw (new Exception("Over 3 times! Quit!"));
16                     Console.WriteLine("Please Input the KeyWord:");
17                 }
18                 Console.WriteLine("Success!");
19             }
20             catch (Exception e)
21             {
22                 Console.WriteLine(e.Message);
23             }            
24             Console.ReadLine();
25 
26             /*
27             Please Input the KeyWord:
28             1
29             Wrong KeyWord!
30             Please Input the KeyWord:
31             2
32             Wrong KeyWord!
33             Please Input the KeyWord:
34             3
35             Wrong KeyWord!
36             Over 3 times! Quit!
37              */
38         }

@5:自定义异常类:

  对于自定义的异常要在出现异常时使用throw关键字来引发异常.

  自定义异常类继承于System.ApplicationException类,该类是区别于异常是系统定义的还是用户自定义的.

示例4
 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             int age = 209;
 6             try
 7             {
 8                 if (age < 0 || age > 200)
 9                     throw (new MyException("年龄超范围", age));
10             }
11             catch (MyException ex)
12             {
13                 Console.WriteLine("catch块: {0}", ex.MyMessage);
14             }
15 
16             Console.WriteLine("age = {0}", age);
17             Console.WriteLine("END!");
18             Console.ReadLine();
19             /*
20             自定义错误:
21             年龄不能大于200
22             catch块: 应用程序中的错误。
23             age = 209
24             END!
25              */
26         }
27 
28     }
29     class MyException : ApplicationException
30     {
31         private string myMessage;
32         public string MyMessage
33         {
34             get { return myMessage; }
35             set
36             {
37                 myMessage = Message;
38                 //myMessage = value;       //Message
39             }
40         }
41         public MyException(string str, int value)
42         {
43             MyMessage = str;
44             Console.WriteLine("自定义错误:");
45             if (value < 0)
46                 Console.WriteLine("年龄不能小于0");
47             else
48                 Console.WriteLine("年龄不能大于200");
49         }
50     }

 

Reference:

吴晓艳《C#语言程序设计》

 

posted @ 2013-04-05 21:30  XiaoweiLiu  阅读(10238)  评论(0编辑  收藏  举报