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

          常用鏈接

          留言簿

          隨筆分類(75)

          隨筆檔案(78)

          相冊

          實用Links

          我的Links

          搜索

          •  

          積分與排名

          • 積分 - 114363
          • 排名 - 515

          最新評論

          閱讀排行榜

          評論排行榜

          ■Exception類的主要成員:
          Exception:構造函數,構造一個異常類,制定其異常消息,發生位置
          Message:只讀屬性,獲取當前異常提供的消息,該消息在構造異常時指定
          Source:讀寫屬性,獲取和設置引起該異常的應用程序或對象的名稱
          TargetSite:只讀屬性,獲取引發該異常的方法
          ToString:公開方法,創建該異常的字符串表示形式,包括異常發生的位置,名稱等信息

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

          namespace ThrowException
          {
              
          class Program
              {
                  
          static void Main(string[] args)
                  {
                      
          try {
                          ThrowAnException();
                      }
          catch(Exception e){
                          ShowException(e);
                      }
                      System.Console.ReadLine();
                  }
                  
          static void ThrowAnException() {
                      
          throw new Exception("This is an Exception!");
                  }
                  
          static void ShowException(Exception e) {
                      System.Console.WriteLine(
          "An Exception information:");
                      System.Console.WriteLine(
          "Type:{0}  ",e.GetType().Name);
                      System.Console.WriteLine(
          "Message:{0}",e.Message);
                      System.Console.WriteLine(
          "Source:{0}",e.Source);
                      System.Console.WriteLine(
          "TargetSite:{0}",e.TargetSite);
                      System.Console.WriteLine(
          "ToString:{0}",e.ToString());
                      System.Console.WriteLine(
          "StackTrace:{0}",e.StackTrace);
                  }
              }
          }


          結果:
          An Exception information:
          ......Type:Exception
          ......Message:This is an Exception!
          ......Source:ThrowException
          ......TargetSite:Void ThrowAnException()
          ......ToString:System.Exception: This is an Exception!
             在 ThrowException.Program.ThrowAnException() 位置 g:"TrueStudy"cSharp"project
          s"ConsoleAppl"ThrowException"Program.cs:行號 20
             在 ThrowException.Program.Main(String[] args) 位置 g:"TrueStudy"cSharp"projec
          ts"ConsoleAppl"ThrowException"Program.cs:行號 13
          ......StackTrace:   在 ThrowException.Program.ThrowAnException() 位置 g:"TrueStu
          dy"cSharp"projects"ConsoleAppl"ThrowException"Program.cs:行號 20
             在 ThrowException.Program.Main(String[] args) 位置 g:"TrueStudy"cSharp"projec
          ts"ConsoleAppl"ThrowException"Program.cs:行號 13



          posted on 2009-10-26 19:06 期待明天 閱讀(1395) 評論(1)  編輯  收藏 所屬分類: CSharp

          FeedBack:
          # re: 異常類Exception 2013-12-15 13:27 常磊
          57預估  回復  更多評論
            
          主站蜘蛛池模板: 河池市| 迁安市| 新兴县| 武威市| 甘南县| 黄石市| 宣威市| 垣曲县| 宜阳县| 大港区| 江源县| 沽源县| 凤城市| 远安县| 根河市| 包头市| 河北省| 兰州市| 楚雄市| 米泉市| 游戏| 武威市| 张家界市| 天门市| 宜兴市| 宜良县| 巨野县| 平定县| 云林县| 和平县| 平利县| 东平县| 萨迦县| 孝义市| 新昌县| 兴国县| 贵州省| 兴仁县| 宜良县| 稻城县| 扶绥县|