📜  C#|如何使用多重catch子句

📅  最后修改于: 2021-05-29 14:54:49             🧑  作者: Mango

catch块的主要目的是处理try块中引发的异常。仅在程序中引发异常时才执行此块。
在C#中,您可以将多个catch块与try块一起使用。通常,多个catch块用于处理不同类型的异常,这意味着每个catch块用于处理不同类型的异常。如果您对同一类型的异常使用多个catch块,那么它将给您一个编译时错误,因为C#不允许您对同一类型的异常使用多个catch块。在catch块之前总是try块。

通常,在程序中按顺序检查catch块。如果给定类型的异常与第一个catch块匹配,则将执行第一个catch块,其余的catch块将被忽略。并且如果起始catch块不适合异常类型,则编译器将搜索下一个catch块。

句法:

try {

// Your code 

}

// 1st catch block
catch(Exception_Name) {

// Code

}

// 2nd catch block
catch(Exception_Name) {

// Code

}

.
.
.
.

下面给出了一些示例,以更好地理解实现:

示例1:在下面的示例中,try块生成两种不同类型的异常,即DivideByZeroExceptionIndexOutOfRangeException 。现在,我们使用两个catch块来处理与单个try块相关联的这些异常。每个捕获块都捕获了不同类型的异常,例如捕获块1用于捕获DivideByZeroException,捕获块2用于捕获IndexOutOfRangeException。

// C# program to illustrate the
// use of multiple catch block
using System;
  
class GFG {
  
    // Main Method
    static void Main()
    {
  
        // Here, number is greater than divisor
        int[] number = { 8, 17, 24, 5, 25 };
        int[] divisor = { 2, 0, 0, 5 };
  
        // --------- try block ---------
  
        for (int j = 0; j < number.Length; j++)
  
            // Here this block raises two different
            // types of exception, i.e. DivideByZeroException
            // and IndexOutOfRangeException
            try {
  
                Console.WriteLine("Number: " + number[j]);
                Console.WriteLine("Divisor: " + divisor[j]);
                Console.WriteLine("Quotient: " + number[j] / divisor[j]);
            }
  
            // Catch block 1
  
            // This Catch block is for
            // handling DivideByZeroException
            catch (DivideByZeroException) {
  
                Console.WriteLine("Not possible to Divide by zero");
            }
  
            // Catch block 2
  
            // This Catch block is for
            // handling IndexOutOfRangeException
            catch (IndexOutOfRangeException) {
                Console.WriteLine("Index is Out of Range");
            }
    }
}
输出:
Number: 8
Divisor: 2
Quotient: 4
Number: 17
Divisor: 0
Not possible to Divide by zero
Number: 24
Divisor: 0
Not possible to Divide by zero
Number: 5
Divisor: 5
Quotient: 1
Number: 25
Index is Out of Range

示例2:在下面的示例中,try块引发异常。因此,我们将使用三种不同类型的catch块来处理try块引发的异常。捕获块1将处理IndexOutOfRangeException ,捕获块2将处理FormatException ,捕获块3将处理OverflowException

// C# program to illustrate the concept
// of multiple catch clause
using System;
  
class GFG {
  
    // Main method
    static void Main()
    {
  
        // This block raises an exception
        try {
  
            byte data = byte.Parse("a");
            Console.WriteLine(data);
        }
  
        // Catch block 1
  
        // This block is used to handle
        // IndexOutOfRangeException type exception
        catch (IndexOutOfRangeException) {
  
            Console.WriteLine("At least provide one Argument!");
        }
  
        // Catch block 2
  
        // This block is used to handle
        // FormatException type exception
        catch (FormatException) {
  
            Console.WriteLine("Entered value in not a number!");
        }
  
        // Catch block 3
  
        // This block is used to handle
        // OverflowException type exception
        catch (OverflowException) {
  
            Console.WriteLine("Data is out of Range!");
        }
    }
}
输出:
Entered value in not a number!