Exception.TargetSite Property
Assembly: mscorlib (in mscorlib.dll)
If the method that throws this exception is not available and the stack trace is not a null reference (Nothing in Visual Basic), TargetSite obtains the method from the stack trace. If the stack trace is a null reference, TargetSite also returns a null reference.
Note: |
|---|
| The TargetSite property may not accurately report the name of the method in which an exception was thrown if the exception handler handles an exception across application domain boundaries. |
The following code example throws an Exception and then catches it and displays the originating method using the TargetSite property.
// Example for the Exception::HelpLink, Exception::Source, // Exception::StackTrace, and Exception::TargetSite properties. using namespace System; namespace NDP_UE_CPP { // Derive an exception; the constructor sets the HelpLink and // Source properties. public ref class LogTableOverflowException: public Exception { private: static String^ overflowMessage = "The log table has overflowed."; public: LogTableOverflowException( String^ auxMessage, Exception^ inner ) : Exception( String::Format( "{0} - {1}", overflowMessage, auxMessage ), inner ) { this->HelpLink = "http://msdn.microsoft.com"; this->Source = "Exception_Class_Samples"; } }; public ref class LogTable { public: LogTable( int numElements ) { logArea = gcnew array<String^>(numElements); elemInUse = 0; } protected: array<String^>^logArea; int elemInUse; public: // The AddRecord method throws a derived exception if // the array bounds exception is caught. int AddRecord( String^ newRecord ) { try { logArea[ elemInUse ] = newRecord; return elemInUse++; } catch ( Exception^ ex ) { throw gcnew LogTableOverflowException( String::Format( "Record \"{0}\" was not logged.", newRecord ),ex ); } } }; // Create a log table and force an overflow. void ForceOverflow() { LogTable^ log = gcnew LogTable( 4 ); 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->ToString() ); } } } int main() { 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." ); NDP_UE_CPP::ForceOverflow(); } /* 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_CPP.LogTable.AddRecord(String newRecord) at NDP_UE_CPP.ForceOverflow() 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 Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: