异常处理示例

  1. using System;
  2. public class DataHouse
  3. {
  4.     public static void FindData( long ID) 
  5.     {
  6.         if( ID>0 && ID<1000)
  7.             Console.WriteLine( ID );
  8.         else
  9.             throw new DataHouseException("已到文件尾");
  10.     }
  11. }
  12. public class BankATM 
  13. {
  14.     public static void GetBalanceInfo( long  ID) 
  15.     {
  16.         try 
  17.         {
  18.             DataHouse.FindData(ID);
  19.         }
  20.         catch (DataHouseException e) 
  21.         {
  22.             throw new MyAppException("账号不存在",e);
  23.         }
  24.     }
  25. }
  26. public class DataHouseException:ApplicationException 
  27. {
  28.     public DataHouseException( string message )
  29.         :base(message)
  30.     {}
  31. }
  32. public class MyAppException:ApplicationException 
  33. {
  34.     public MyAppException (string message) 
  35.         base (message) 
  36.     {}
  37.     public MyAppException (string message, Exception inner)
  38.         base(message,inner)
  39.     {}   
  40. }
  41. public class Test 
  42. {
  43.     public static void Main() 
  44.     {
  45.         try 
  46.         {
  47.             BankATM.GetBalanceInfo( 12345L);
  48.         }
  49.         catch(Exception e) 
  50.         {
  51.             Console.WriteLine ("出现了异常: {0}", e.Message);
  52.             Console.WriteLine ("内部原因: {0}",e.InnerException.Message);
  53.         }
  54.     }
  55. }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值