分享

Exception.TargetSite 属性 (System)

 冷泉阁 2016-10-14

https://msdn.microsoft.com/zh-cn/library/system.exception.targetsite(v%3DVS.80).aspx

Exception.TargetSite 属性

.NET Framework 2.0

获取引发当前异常的方法。

命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)

public MethodBase TargetSite { get; }
/** @property */
public final MethodBase get_TargetSite ()

public final function get TargetSite () : MethodBase

属性值

引发当前异常的 MethodBase

如果引发此异常的方法不可用且堆栈跟踪不是空引用(Visual Basic 中为 Nothing),则 TargetSite 从堆栈跟踪中获取该方法。如果堆栈跟踪是空引用,则 TargetSite 也将返回空引用。

下面的代码示例引发一个 Exception,然后捕捉该异常,并使用 TargetSite 属性显示源发方法。

// Example for the Exception.HelpLink, Exception.Source,
// Exception.StackTrace, and Exception.TargetSite properties.
using System;

namespace NDP_UE_CS
{
    // Derive an exception; the constructor sets the HelpLink and 
    // Source properties.
    class LogTableOverflowException : Exception
    {
        const string overflowMessage = "The log table has overflowed.";

        public LogTableOverflowException( 
            string auxMessage, Exception inner ) :
                base( String.Format( "{0} - {1}", 
                    overflowMessage, auxMessage ), inner )
        {
            this.HelpLink = "http://msdn.microsoft.com";
            this.Source = "Exception_Class_Samples";
        }
    }

    class LogTable
    {
        public LogTable( int numElements )
        {
            logArea = new string[ numElements ];
            elemInUse = 0;
        }

        protected string[ ] logArea;
        protected int       elemInUse;

        // The AddRecord method throws a derived exception if 
        // the array bounds exception is caught.
        public    int       AddRecord( string newRecord )
        {
            try
            {
                logArea[ elemInUse ] = newRecord;
                return elemInUse++;
            }
            catch( Exception e )
            {
                throw new LogTableOverflowException( 
                    String.Format( "Record \"{0}\" was not logged.", 
                        newRecord ), e );
            }
        }
    }

    class OverflowDemo 
    {
        // Create a log table and force an overflow.
        public static void Main() 
        {
            LogTable log = new LogTable( 4 );

            Console.WriteLine( 
                "This example of \n   Exception.Message, \n" +
                "   Exception.HelpLink, \n   Exception.Source, \n" +
                "   Exception.StackTrace, and \n   Exception." +
                "TargetSite \ngenerates the following output." );

            try
            {
                for( int count = 1; ; count++ )
                {
                    log.AddRecord( 
                        String.Format( 
                            "Log record number {0}", count ) );
                }
            }
            catch( Exception ex )
            {
                Console.WriteLine( "\nMessage ---\n{0}", ex.Message );
                Console.WriteLine( 
                    "\nHelpLink ---\n{0}", ex.HelpLink );
                Console.WriteLine( "\nSource ---\n{0}", ex.Source );
                Console.WriteLine( 
                    "\nStackTrace ---\n{0}", ex.StackTrace );
                Console.WriteLine( 
                    "\nTargetSite ---\n{0}", ex.TargetSite );
            }
        }
    }
}

/*
This example of
   Exception.Message,
   Exception.HelpLink,
   Exception.Source,
   Exception.StackTrace, and
   Exception.TargetSite
generates the following output.

Message ---
The log table has overflowed. - Record "Log record number 5" was not logged.

HelpLink ---
http://msdn.microsoft.com

Source ---
Exception_Class_Samples

StackTrace ---
   at NDP_UE_CS.LogTable.AddRecord(String newRecord)
   at NDP_UE_CS.OverflowDemo.Main()

TargetSite ---
Int32 AddRecord(System.String)
*/

// Example for the Exception.HelpLink, Exception.Source,
// Exception.StackTrace, and Exception.TargetSite properties.
package NDP_UE_JSL; 

import System.* ;

// Derive an exception; the constructor sets the HelpLink and 
// Source properties.
class LogTableOverflowException extends System.Exception
{
    private String overflowMessage = "The log table has overflowed.";

    public LogTableOverflowException(System.String auxMessage, 
        System.Exception inner)
    {
        super(String.Format("The log table has overflowed. - {0}", 
            auxMessage), inner);
        this.set_HelpLink("http://msdn.microsoft.com");
        this.set_Source("Exception_Class_Samples");
    } //LogTableOverflowException
} //LogTableOverflowException

class LogTable
{
    public LogTable(int numElements)
    {
        logArea = new String[numElements];
        elemInUse = 0;
    } //LogTable

    protected String logArea[];
    protected int elemInUse;

    // The AddRecord method throws a derived exception if 
    // the array bounds exception is caught.
    public int AddRecord(String newRecord) throws LogTableOverflowException
    {
        try {
            logArea.set_Item(elemInUse, newRecord);
            return elemInUse++;
        }
        catch (System.Exception e) {
            throw new LogTableOverflowException(
                String.Format("Record \"{0}\" was not logged.", newRecord), e);
        }
    } //AddRecord
} //LogTable

class OverflowDemo
{
    // Create a log table and force an overflow.
    public static void main(String[] args)
    {
        LogTable log = new LogTable(4);

        Console.WriteLine(("This example of \n   Exception.Message, \n" 
            + "   Exception.HelpLink, \n   Exception.Source, \n" 
            + "   Exception.StackTrace, and \n   Exception." 
            + "TargetSite \ngenerates the following output."));
        
        try {
            for (int count = 1; ; count++) {
                log.AddRecord(String.Format("Log record number {0}", 
                    System.Convert.ToString(count)));
            }
        }
        catch (System.Exception ex) {
            Console.WriteLine("\nMessage ---\n{0}", ex.get_Message());
            Console.WriteLine("\nHelpLink ---\n{0}", ex.get_HelpLink());
            Console.WriteLine("\nSource ---\n{0}", ex.get_Source());
            Console.WriteLine("\nStackTrace ---\n{0}", ex.get_StackTrace());
            Console.WriteLine("\nTargetSite ---\n{0}", ex.get_TargetSite());
        }
    } //main
} //OverflowDemo
   
/*
   Exception.Message,
   Exception.HelpLink,
   Exception.Source,
   Exception.StackTrace, and
   Exception.TargetSite
generates the following output.

Message ---
The log table has overflowed. - Record "Log record number 5" was not logged.

HelpLink ---
http://msdn.microsoft.com

Source ---
Exception_Class_Samples

StackTrace ---
   at NDP_UE_JSL.LogTable.AddRecord(String newRecord) in D:\Test\ConsoleApplicat
ion7\ConsoleApplication7\Class1.jsl:line 45
   at NDP_UE_JSL.OverflowDemo.main(String[] args) in D:\Test\ConsoleApplication7
\ConsoleApplication7\Class1.jsl:line 62

TargetSite ---
Int32 AddRecord(System.String)
 */

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多