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

          常用鏈接

          留言簿

          隨筆分類(75)

          隨筆檔案(78)

          相冊

          實用Links

          我的Links

          搜索

          •  

          積分與排名

          • 積分 - 114333
          • 排名 - 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預估  回復  更多評論
            
          主站蜘蛛池模板: 江西省| 上高县| 邵东县| 屏边| 鄂托克前旗| 漠河县| 类乌齐县| 靖西县| 新绛县| 荥阳市| 江陵县| 馆陶县| 萍乡市| 巴彦淖尔市| 横峰县| 丽水市| 原平市| 宁河县| 高碑店市| 军事| 华蓥市| 黄冈市| 铜山县| 商都县| 镇巴县| 孙吴县| 紫阳县| 芜湖市| 宾川县| 拜泉县| 台江县| 康平县| 汕尾市| 中西区| 嘉善县| 平湖市| 宝清县| 苍山县| 修武县| 封开县| 建始县|