Nesting of try and catch blocks

ณัฐพล สุพรรณมี 650710983

Nesting of try and catch blocks คืออะไร?

ใน C# นี่อนุญาตให้ทำบล็อก try และ catch ซ้อนกันได้ ซึ่งหมายความว่าบล็อก try หนึ่งสามารถอยู่ในอีกบล็อกหนึ่งได้ โปรแกรมเมอร์มักจะใช้บล็อก try ข้างนอกเพื่อจัดการกับข้อผิดพลาดที่ร้ายแรง ในขณะที่บล็อกข้างในจะใช้สำหรับจัดการกับข้อผิดพลาดทั่วไป

Note:

  • ถ้ามีข้อผิดพลาดเกิดขึ้นในบล็อก try ข้างในที่ไม่ได้ถูกจับโดยบล็อก catch ที่เกี่ยวข้อง ข้อผิดพลาดนั้นจะถูกส่งต่อไปยังบล็อก try ข้างนอก โดยทั่วไปแล้ว บล็อก try ซ้อนจะใช้เพื่อให้สามารถจัดการกับกลุ่มข้อผิดพลาดต่างๆ ในวิธีที่แตกต่างกันได้.

  • เป็นเงื่อนไขที่จำเป็นว่าบล็อก try ต้องตามด้วยบล็อก catch หรือ finally เพราะถ้าคุณใช้บล็อก try โดยไม่มี catch หรือ finally จะทำให้เกิดข้อผิดพลาดในการคอมไพล์.

Syntax:

// outer try block
try
{

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

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

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

ด้านล่างนี้มีตัวอย่างบางอย่างที่จะช่วยให้เข้าใจการใช้งานได้ดีขึ้น:

ตัวอย่างใน C#

ตัวอย่างนี้แสดงการใช้บล็อก catch ซ้อนกันใน C# ซึ่งจะสร้างข้อผิดพลาด DivideByZeroException ในบล็อก try ภายใน ซึ่งจะถูกจับโดยบล็อก catch ภายใน แต่ถ้ามีข้อผิดพลาด IndexOutOfRangeException เกิดขึ้น มันจะถูกส่งต่อไปยังบล็อก try ภายนอก ซึ่งบล็อก catch ภายนอกจะจับข้อผิดพลาดนั้น

using System;

class GFG {
    static void Main() 
    { 
        int[] number = {8, 17, 24, 5, 25}; 
        int[] divisor = {2, 0, 0, 5};

        try {
            for (int j = 0; j < number.Length; j++) {
                try { 
                    Console.WriteLine("Number: " + number[j] + 
                                      "\nDivisor: " + divisor[j] + 
                                      "\nQuotient: " + number[j] / divisor[j]); 
                } 
                catch (DivideByZeroException) { 
                    Console.WriteLine("Inner Try Catch Block"); 
                } 
            } 
        } 
        catch (IndexOutOfRangeException) { 
            Console.WriteLine("Outer Try Catch Block"); 
        } 
    } 
}

การใช้ Nested try-catch ในภาษาอื่น

Java:

ใน Java เราก็สามารถใช้บล็อก try-catch ซ้อนกันได้ โดยโครงสร้างการทำงานคล้ายกับ C# แต่แตกต่างกันเล็กน้อยในแง่ของ syntax

public class GFG {
    public static void main(String[] args) {
        int[] number = {8, 17, 24, 5, 25};
        int[] divisor = {2, 0, 0, 5};

        try {
            for (int j = 0; j < number.length; j++) {
                try {
                    System.out.println("Number: " + number[j] +
                                       "\nDivisor: " + divisor[j] +
                                       "\nQuotient: " + number[j] / divisor[j]);
                } catch (ArithmeticException e) {
                    System.out.println("Inner Try Catch Block");
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Outer Try Catch Block");
        }
    }
}

Python:

ใน Python การจัดการข้อผิดพลาดทำได้โดยใช้บล็อก try-except ซึ่งสามารถซ้อนกันได้เหมือนใน C# และ Java โดยโครงสร้างไวยากรณ์จะยืดหยุ่นมากกว่า

number = [8, 17, 24, 5, 25]
divisor = [2, 0, 0, 5]

try:
    for j in range(len(number)):
        try:
            print(f"Number: {number[j]}\nDivisor: {divisor[j]}\nQuotient: {number[j] // divisor[j]}")
        except ZeroDivisionError:
            print("Inner Try Catch Block")
except IndexError:
    print("Outer Try Catch Block")

การเปรียบเทียบระหว่าง C#, Java และ Python

C#

  • ต้องมีบล็อก catch หรือ finally หลัง try เสมอ

  • มีการจับข้อผิดพลาดแบบเฉพาะเจาะจง เช่น DivideByZeroException หรือ IndexOutOfRangeException

Java

  • โครงสร้างคล้ายกับ C# แต่มีความเข้มงวดในเรื่องของชนิดของ exception ที่จะจับ

  • มีคลาสข้อผิดพลาดเฉพาะมากมาย เช่น ArithmeticException และ ArrayIndexOutOfBoundsException

Python

  • ไวยากรณ์ง่ายและยืดหยุ่นกว่ามาก

  • ใช้ except เพื่อจับข้อผิดพลาด โดยสามารถใช้จับข้อผิดพลาดเฉพาะหรือจับข้อผิดพลาดทั่วไปได้

Video Presentation

Presentation

File

แหล่งที่มา

Last updated