码迷,mamicode.com
首页 > Windows程序 > 详细

C#中的异常处理

时间:2015-04-21 00:14:22      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

    今天我们研究学习一下C#中的异常处理机制。

    首先,异常处理是用来防止可以预测到却不能完全防止的错误。使用得当的异常处理机制,可以有效防止程序的崩溃。在这种意义上,程序其他的一些错误,诸如bugs, errors需要用应用程序来处理用户的错误。

    异常处理有以下几个字段:try,throw,catch。使用异常处理,要用System.Exception的命名空间。抛出的异常,会沿着类的继承链条延续下去,直到传到System.Exception中去(这种机制同C++和JAVA一样)。

 1 public class Test
 2 {
 3    public static void Main()
 4    {
 5       Console.WriteLine("Enter Main...");
 6       Test t = new Test();
 7       t.Func1();
 8       Console.WriteLine("Exit Main...");
 9    }
10    public void Func1()
11    {
12        Console.WriteLine("Enter Func1...");
13        Func2();
14        Console.WriteLine("Exit Func1...");
15     }
16 
17 public void Func2()
18 {
19   Console.WriteLine("Enter Func2...");
20   throw new System.ApplicationException();
21   Console.WriteLine("Exit Func2...");
22   }
23  }
24 }

 

C#中的异常处理

标签:

原文地址:http://www.cnblogs.com/Dukechris/p/4443000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!