C# Exception TargetSite

Description

Exception TargetSite gets the method that throws the current exception.

Syntax

Exception.TargetSite has the following syntax.


public MethodBase TargetSite { get; }

Example

The following code example throws an Exception and then catches it and displays the originating method using the TargetSite property.


/*ww w . j  a  v a2s.co  m*/
using System;

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://java2s.com";
        this.Source = "Exception_Class_Samples";
    }
}

class OverflowDemo 
{
    public static void Main() 
    {
        try
        {
            throw new LogTableOverflowException( 
                String.Format( "Record \"{0}\" was not logged.", 
                    "aaa"), new Exception("asdf") );

        }
        catch( Exception ex )
        {

            Console.WriteLine( 
                "\nTargetSite ---\n{0}", ex.TargetSite );
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version