skywalker2909
skywalker2909

Reputation: 1686

custom exception message not displaying in the console

I have this simple program demonstrating exception handling in .NET, i am catching the ArgumentOutOfRangeException in this case, but the custom message that i pass to the console is not getting displayed.

using System;

class program
{
static void Main()
{
    int[] source = { 1, 2, 3, 4, 5 };
    int[] destination = { 6, 7, 8, 9 };

    try
    {
        Array.Copy(source, destination, 7);
    }
    catch (ArgumentOutOfRangeException e)
    {
        Console.WriteLine("Sorry, there is something wrong in the program ! : {0}", e.Message);
    }
}
}

and here is the output screenshot

enter image description here

Upvotes: 1

Views: 394

Answers (1)

SLaks
SLaks

Reputation: 887479

You're catching ArgumentOutOfRangeException, but the method is throwing an ArgumentException.
Therefore, your catch block never executes.

Upvotes: 8

Related Questions