Exception处理机制

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication2
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 //Level1
13 try
14 {
15 //Level2
16 try
17 {
18
19 //Level3
20 try
21 {
22 //throw new ExceptionA("Exception in Level3");
23 //throw new ExceptionB("Exception in Level3");
24 }
25 catch (ExceptionA)
26 {
27 Console.WriteLine("Level3中处理Exception");
28 }
29 finally
30 {
31 Console.WriteLine("Level3中的finally语句块");
32 }
33 throw new ExceptionC("Exception in Level2");
34 }
35 catch (ExceptionB)
36 {
37 Console.WriteLine("Level2中处理Exception");
38 }
39 finally
40 {
41 Console.WriteLine("Level2中的finally语句块");
42 }
43 //throw new Exception("Exception in Level1");
44 }
45 catch (ExceptionC)
46 {
47 Console.WriteLine("Level1中处理ExceptionC");
48 }
49 catch (Exception)
50 {
51 Console.WriteLine("Level1中处理Exception");
52 }
53 finally
54 {
55 Console.WriteLine("Level1中的finally语句块");
56 }
57
58
59 Console.ReadKey();
60
61 }
62 }
63 }
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication2
7 {
8 class ExceptionA : Exception
9 {
10 public ExceptionA(string mess):base(mess)
11 {
12 }
13 }
14 }

首先在同级查找是否存在兼容的异常,如果没有,则一级一级往上

原文地址:https://www.cnblogs.com/changweihua/p/2171980.html