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

常用链接

留言簿

随笔分类(75)

随笔档案(78)

相册

实用Links

我的Links

搜索

  •  

积分与排名

  • 积分 - 111807
  • 排名 - 519

最新评论

阅读排行榜

评论排行榜

■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 期待明天 阅读(1358) 评论(1)  编辑  收藏 所属分类: CSharp

FeedBack:
# re: 异常类Exception 2013-12-15 13:27 常磊
57预估  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: