C#代码异常

本文关键字:异常 代码 | 更新日期: 2023-09-27 18:00:39

我是c#的初学者,我不知道这段代码出了什么问题,需要的帮助

        try 
        {
            double[,] matrix = new double[2,2];
            String liczba = "85481";
            matrix[1,1] = double.Parse(liczba); 
        }
        catch (Exception)
        {
            Console.WriteLine ("general exception");
        }
        catch (OverflowException)
        {
            Console.WriteLine ("exceeded scope of variable");
        }
        catch (FormatException)
        {
            Console.WriteLine ("variable converstion error");
        }

C#代码异常

编译器在这里帮了你一点忙。你会有两个错误,看起来像这样:

先前的catch子句已经捕获了此或超类型("System.Exception")的所有异常

在不太特定的类型之后,无法捕获更特定类型的Exception。从C#参考,重点挖掘:

catch子句的顺序很重要,因为catch子句是按顺序检查的。在不太具体的异常之前捕获更具体的异常如果您对catch块进行排序,使得以后的块永远无法访问,编译器将产生错误

所有异常都源自ExceptionSystem.Exception)。重新命令他们将Exception的处理程序作为最后一个catch子句,它将编译:

try
{
    double[,] matrix = new double[2, 2];
    String liczba = "85481";
    matrix[1, 1] = double.Parse(liczba);
}        
catch (OverflowException)
{
    Console.WriteLine("exceeded scope of variable");
}
catch (FormatException)
{
    Console.WriteLine("variable converstion error");
}
catch (Exception)
{
    Console.WriteLine("general exception");
}