自定义异常消息未显示在控制台中

Nev*_*lle 1 .net c# exception-handling exception

我有这个简单的程序演示.NET中的异常处理,我ArgumentOutOfRangeException在这种情况下捕获,但我传递给控制台的自定义消息没有显示.

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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

这是输出截图

在此输入图像描述

SLa*_*aks 8

你抓到了ArgumentOutOfRangeException,但方法是抛出一个ArgumentException.
因此,您的catch块永远不会执行.