隨筆 - 78  文章 - 25  trackbacks - 0
          <2009年10月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿

          隨筆分類(75)

          隨筆檔案(78)

          相冊

          實用Links

          我的Links

          搜索

          •  

          積分與排名

          • 積分 - 114333
          • 排名 - 515

          最新評論

          閱讀排行榜

          評論排行榜

          ■異常與錯誤
          異常是可預見,可接受的,程序通過對異常的捕獲和處理可以將異常帶來的影響減小到最小;
          錯誤是程序代碼的錯誤,設計漏洞,是不可預見的,會給軟件帶來致命的影響
          ■捕獲異常:try...catch
          ■拋出異常 throw語句

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;

          namespace useException
          {
              
          class Program
              {
                  
          static void Main(string[] args)
                  {
                      
          int val;
                      
          for (int i = -3; i < 12; i++) {
                          
          try {
                              val
          =GetValue(i);
                              System.Console.WriteLine(
          "val:{0}",val);
                          }
          catch(Exception e){
                              System.Console.WriteLine(
          "exception:{0}: ",e.Message);
                          }                
                      }
                      System.Console.ReadLine();
                  }
                  
          static int GetValue(int index)
                  {
                      
          int[] ary = {0,1,2,3,4,5,6,7,8 };
                      
          if (index < 0) {
                          System.Console.Write(
          "index={0}   ",index);
                          
          throw new Exception("index<0");
                      }
                      
          if (index > 8){
                          System.Console.Write(
          "index={0}    ", index);
                          
          throw new Exception("index>8");
                      }
                      
          return ary[index];
                  }
              }
          }

          結果:

          index=-3   exception:index<0:
          index=-2   exception:index<0:
          index=-1   exception:index<0:
          val:0
          val:1
          val:2
          val:3
          val:4
          val:5
          val:6
          val:7
          val:8
          index=9    exception:index>8:
          index=10    exception:index>8:
          index=11    exception:index>8:


          posted on 2009-10-26 18:47 期待明天 閱讀(216) 評論(0)  編輯  收藏 所屬分類: CSharp
          主站蜘蛛池模板: 泰安市| 牙克石市| 绥江县| 集安市| 伊宁县| 神农架林区| 建瓯市| 广饶县| 黄龙县| 黄骅市| 湖南省| 巴南区| 府谷县| 镇雄县| 墨竹工卡县| 布拖县| 张家界市| 贵阳市| 郴州市| 金寨县| 马边| 商城县| 东丰县| 曲松县| 仪陇县| 松阳县| 岳池县| 南阳市| 神池县| 汝南县| 景谷| 南平市| 邻水| 米林县| 肥西县| 宜昌市| 天门市| 西昌市| 娱乐| 文水县| 枣庄市|