关于C#异常处理!

 1 using System;
 2 
 3 namespace ConsoleApplication2
 4 {
 5  /// <summary>
 6  /// Class1 的摘要说明。
 7  /// </summary>
 8  class Class1
 9  {
10   static string[] eTypes={"none","simple","index","nested index"};
11   [STAThread]
12   static void Main(string[] args)
13   {
14    foreach(string eType in eTypes)
15    {
16     try
17     {
18      Console.WriteLine("Main() try block reached.");
19      Console.WriteLine("ThrowException(\"{0}\") called.",eType);
20      ThrowException(eType);
21      Console.WriteLine("Main() try block continues.");
22     }
23     catch(System.IndexOutOfRangeException e)
24     {
25      Console.WriteLine("Main() System.IndexOutOfRangeException catch "+
26       " block reched.Message:\n\"{0}\"",e.Message);
27     }
28     catch
29     {
30      Console.WriteLine("Main() general catch block reached.");
31     }
32     finally
33     {
34      Console.WriteLine("Main() finally block reached.");
35     }
36     Console.WriteLine();
37    }
38   }
39   static void ThrowException(string exceptionType)
40   {
41    Console.Write("ThrowException(\"{0}\")reached.",exceptionType);
42    switch(exceptionType)
43    {
44     case "none":
45      Console.WriteLine("Not throwing an exception.");
46      break;
47     case "simple":
48      Console.WriteLine("Throwing System.Exception.");
49      throw(new System.Exception());
50      break;
51     case "index":
52      Console.WriteLine("Throwing System.IndexOutOfRangeexception.");
53      eTypes[4]="error";
54      break;
55     case "nested index":
56      try
57      {
58       Console.WriteLine("ThrowException(\"nested index\"):"+"tyr block reached.");
59       Console.WriteLine("ThrowException(\"index\")called.");
60       ThrowException("index");
61      }
62      catch
63      {
64       Console.WriteLine("ThrowException(\"nested index\")general"+" catch block reached.");
65      }
66      finally
67      {
68       Console.WriteLine("ThrowException(\"nested index\")finally"+"block reached.");
69      }
70      break;
71    }
72   }
73  }
74 }
75 
76 此异常的处理说明,异常产生后,如果在当前未做Try处理,则会返到上一级,如果还未做,则返回上一级的上一级.以此类推.如果被任意一级Catch到,则上一级不会再得到.
posted @ 2006-08-08 16:49  吴东雷  阅读(324)  评论(0编辑  收藏  举报