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.