Exception TargetSite Property in C#

 

The System.Exception.TargetSite property is used to find out the details about the method which threw the exception. That means TargetSite property of Exception returns all details like parameters, type of parameters, return type...etc of the method whenever it causes any exception.

 

We can use TargetSite property as shown below.

 

class Program

{

        static void Main(string[] args)

        {

            try

            {

 

            }

            catch (Exception ex)

            {

                Console.WriteLine("\n*** Error! ***");

                Console.WriteLine("Member name: {0}", ex.TargetSite);

                Console.WriteLine("Class defining member: {0}", ex.TargetSite.DeclaringType);

                Console.WriteLine("Member type: {0}", ex.TargetSite.MemberType);

                Console.WriteLine("Message: {0}", ex.Message);

                Console.WriteLine("Source: {0}", ex.Source);

            }

 

            Console.ReadLine();

        }

}

 

As shown above TargetSite has several other properties to provides the method details.