<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->■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);
}
}
}
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