Description





Description

The TargetInvocationException constructors are passed a reference to the exception thrown by the invoked method. [Note: The InnerException property inherited from System.Exception holds the exception.]

Example

using System;

using System.Reflection;



/// <summary>

/// Shows why a TargetInvocationException is raised

/// and how to handle it

/// </summary>



public class TargetInvocationExceptionSample

{

    public static void Main()

    {

        try

        {

            Type type = typeof(string);

            string str = "do unto others..";

            string result = (string)type.InvokeMember("Remove",

                BindingFlags.InvokeMethod, null, str, new object[] { 7, 60 });

            Console.WriteLine(result);

        }

        catch (TargetInvocationException e)

        {

            Console.WriteLine("Caught exception: {0} ", e.GetType().Name);

            Console.WriteLine ("     {0} - '{1}'",

                e.InnerException.GetType().Name, e.InnerException.Message);

        }

        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue");

        Console.ReadLine();

    }

}


The output is


Caught exception: TargetInvocationException

     ArgumentOutOfRangeException - 'Index and count must refer to a location

within the string.

Parameter name: count'





Press Enter to continue



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows