异常类Exception

<!--<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);
        }
    }
}


结果:
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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值