Exception

Posted on 2019-03-17 11:17  金色的省略号  阅读(158)  评论(0编辑  收藏  举报
 1 using System;
 2 
 3 public class DataHouse
 4 {
 5     public static void FindData( long ID) 
 6     {
 7         if( ID>0 && ID<1000)
 8             Console.WriteLine( ID );
 9         else
10             throw new DataHouseException("已到文件尾");
11     }
12 }
13 
14 public class BankATM 
15 {
16     public static void GetBalanceInfo( long  ID) 
17     {
18         try 
19         {
20             DataHouse.FindData(ID);
21         }
22         catch (DataHouseException e) 
23         {
24             throw new MyAppException("账号不存在",e);
25         }
26     }
27 }
28 
29 //DataHouseException
30 public class DataHouseException:ApplicationException 
31 {
32     public DataHouseException( string message )
33         :base(message)
34     {}
35 }
36 
37 public class MyAppException:ApplicationException 
38 {
39     public MyAppException (string message) 
40         : base (message) 
41     {}
42 
43     public MyAppException (string message, Exception inner)
44         : base(message,inner)
45     {}   
46 }
47 
48 public class Test 
49 {
50     public static void Main() 
51     {
52         try 
53         {
54             BankATM.GetBalanceInfo( 12345L);
55         }
56         catch(Exception e) 
57         {
58             Console.WriteLine ("出现了异常: {0}", e.Message);
59 
60             Console.WriteLine ("内部原因: {0}",e.InnerException.Message);
61         }
62     }
63 }