Mong mọi người dịch & giải thích em đoạn này trong Java

Em đang đọc sách hướng dẫn về Java thì gặp đoạn sau, em đọc mấy lần nhưng không hiểu mong các anh giúp em

12.4.3 Catching Exceptions
You now know how to declare an exception and how to throw an exception. When an exception is thrown, it can be caught and handled in a try-catch block, as follows:

try {
    statements; // Statements that may throw exceptions
}
catch (Exception1 exVar1) {
    handler for exception1;
}
catch (Exception2 exVar2) {
    handler for exception2;
}
    ...
catch (ExceptionN exVarN) {
    handler for exceptionN;
}

If no exceptions arise during the execution of the try block, the catch blocks are skipped. If one of the statements inside the try block throws an exception, Java skips the remaining statements in the try block and starts the process of finding the code to handle the exception. The code that handles the exception is called the exception handler; it is found by propagating the exception backward through a chain of method calls, starting from the current method. Each catch block is examined in turn, from first to last, to see whether the type of the exception object is an instance of the exception class in the catch block.
If so, the exception object is assigned to the variable declared, and the code in the catch block is executed. If no handler is found, Java exits this method, passes the exception to the method that invoked the method, and continues the same process to find a handler. If no handler is found in the chain of methods being invoked, the program terminates and prints an error message on the console. The process of finding a handler is called catching an exception.

Đơn giản dễ hiểu thế này thôi:

Nếu như trong một method xuất hiện exception ở dòng thứ x

  1. Nếu như exception đó xảy ra không ở trong khối try thì method kết thúc tại dòng x và exception được ném lên cho method gọi method đó.

  2. Nếu như exception đó xảy ra ở trong khối try nhưng không có khối catch nào bắt exception này thì thực hiện tương tự như (1)

  3. Nếu như exception đó xảy ra ở trong khối try, khối catch đầu tiên bắt được exception đó sẽ được thực hiện. Sau khi khối catch đó thực hiện xong thì hàm chạy tiếp sau khối try-catch đó.

  4. Nếu như sau khi ném lên nhiều lần ở (1) đến tận phương thức main() mà vẫn không có khối catch nào bắt được exception đó thì exception này được ném ra hệ điều hành, chương trình bị crash và chấm dứt.

4 Likes

Sách viết hơi dài dòng. Bạn có thể hiểu đơn giản là trong khối lệnh try có thể có nhiều lỗi khác nhau, mỗi lỗi tương ứng với một Exception. Nếu catch đầu tiên không bắt được lỗi này thì chuyển sang catch 2, tương tự JVM sẽ kiểm tra lần lượt từng catch cho đến khi tìm được catch phù hợp với exception được throw ra từng try. Khi đó khối lệnh trong catch sẽ được thực thi và sẽ không kiểm tra catch khác, chương trình tiếp tục chạy bình thường.

Nếu vẫn không có catch nào phù hợp exception được throw ra từng try thì chương trình có thể sẽ chết.

Chi tiết về exception trong Java bạn có thể xem thêm link này: https://gpcoder.com/2430-xu-ly-ngoai-le-trong-java-exception-handling/

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?