📜  C#|尝试和捕获块的嵌套

📅  最后修改于: 2021-05-29 18:13:08             🧑  作者: Mango

在C#中,允许嵌套try&catch块。 try块的嵌套意味着一个try块可以嵌套到另一个try块中。各种程序员使用外部try块来处理严重异常,而使用内部block来处理正常异常。

笔记:

  • 如果内部try块中引发了异常,而该异常没有被与try块相关联的catch块捕获,则该异常将提升为外部try块。通常,嵌套的try块用于允许以不同的方式处理错误的不同组。
  • 在try块之后必须有catch或finally块是一个必要条件,因为如果您使用没有catch或try的try块,则将容易产生编译时错误。

句法:

// outer try block
try
{

   // inner try block
   try
     {
       // code...
     }

   // inner catch block
   catch
     {
       // code...
     }
}

// outer catch block
catch
  {
    // code...
  }

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

示例1:在此程序中,在由与内部try块相关联的catch块捕获的内部try块内生成DivideByZeroException,并继续执行程序。IndexOutOfRangeException在内部try块内生成而未被内部catch块捕获时,则内部try块会将此异常传输到外部try块。此后,与外部try块关联的catch块捕获了导致程序终止的异常。在这里,对于17/024/0,内部try-catch块正在执行,但是对于编号25,外部try-catch块正在执行。

// C# program to illustrate how outer
// try block will handle the exception
// which is not handled by the inner 
// try 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};
  
        // Outer try block
          
        // Here IndexOutOfRangeException occurs
        // due to which program may terminates
        try {
              
            for (int j = 0; j < number.Length; j++) {
  
                // Inner try block
                  
                // Here DivideByZeroException caught
                // and allow the program to continue 
                // its execution
  
                try {
                      
                    Console.WriteLine("Number: " + number[j] + 
                                      "\nDivisor: " + divisor[j] + 
                                      "\nQuotient: " + number[j] / divisor[j]);
                }
                  
                // Catch block for inner try block
                catch (DivideByZeroException) {
                      
                    Console.WriteLine("Inner Try Catch Block");
                }
            }
        }
  
        // Catch block for outer try block
        catch (IndexOutOfRangeException) {
              
            Console.WriteLine("Outer Try Catch Block");
              
        }
    }
}
输出:
Number: 8
Divisor: 2
Quotient: 4
Inner Try Catch Block
Inner Try Catch Block
Number: 5
Divisor: 5
Quotient: 1
Outer Try Catch Block

示例2:在下面的示例中,内部try块内生成了一个异常,该异常由与内部try块相关联的catch块捕获。

// C# program to illustrate
// nested try block
using System;
  
public class Geeks {
  
    public string Author_name
    {
        get;
        set;
    }
}
  
// Driver Class
public class GFG {
      
      
    // Main Method
    public static void Main()
    {
        Geeks str1 = null;
  
        // outer try block
        try {
              
            // inner try block
            try {
                  
                str1.Author_name = "";
            }
  
            // catch block for the inner try block
            catch {
                  
                Console.WriteLine("Inner try catch block");
            }
        }
          
        // catch block for the outer try block
        catch {
              
            Console.WriteLine("Outer try catch block");
        }
    }
}
输出:
Inner try catch block