Bài 76: Khối lệnh finally trong java

Ngày đăng: 1/2/2023 2:50:11 PM

Tại sao phải sử dụng khối finally

Khối finally có thể được sử dụng để chèn lệnh "cleanup" vào chương trình như việc đóng file, đóng các kết nối,...


Cách sử dụng khối finally trong java

Dưới đây là các trường hợp khác nhau về việc sử dụng khối finally trong java.

TH1

sử dụng khối lệnh finally nơi ngoại lệ không xảy ra.

1

2

3

4

5

6

7

8

9

10

11

12   

13

public class TestFinallyBlock {

    public static void main(String args[]) {

        try {

            int data = 25 / 5;

            System.out.println(data);

        } catch (NullPointerException e) {

            System.out.println(e);

        } finally {

            System.out.println("finally block is always executed");

        }

        System.out.println("rest of the code...");

    }

}

Output:

 5
 finally block is always executed
 rest of the code...
 

TH2

sử dụng khối lệnh finally nơi ngoại lệ xảy ra nhưng không xử lý.

1

2

3

4

5

6

7

8

9

10

11

12

13    

public class TestFinallyBlock1 {

    public static void main(String args[]) {

        try {

            int data = 25 / 0;

            System.out.println(data);

        } catch (NullPointerException e) {

            System.out.println(e);

        } finally {

            System.out.println("finally block is always executed");

        }

        System.out.println("rest of the code...");

    }

}

Output:

 finally block is always executed
 Exception in thread "main" java.lang.ArithmeticException: / by zero
 

TH3

sử dụng khối lệnh finally nơi ngoại lệ xảy ra và được xử lý.

1

2

3

4

5

6

7

8

9

10

11

12

13    

public class TestFinallyBlock2 {

    public static void main(String args[]) {

        try {

            int data = 25 / 0;

            System.out.println(data);

        } catch (ArithmeticException e) {

            System.out.println(e);

        } finally {

            System.out.println("finally block is always executed");

        }

        System.out.println("rest of the code...");

    }

}

Output:

 java.lang.ArithmeticException: / by zero
 finally block is always executed
 rest of the code...
 

TH4

Sử dụng khối lệnh finally trong trường hợp trong khối try có lệnh return.

1

2

3

4

5

6

7

8

9

10

11

12    

13

14

15

16

public class TestFinallyBlock3 {

    public static void main(String args[]) {

        try {

            int data = 25;

            if (data % 2 != 0) {

             System.out.println(data + " is odd number");

             return;

            }

        } catch (ArithmeticException e) {

            System.out.println(e);

        } finally {

            System.out.println("finally block is always executed");

        }

        System.out.println("rest of the code...");

    }

}

Output:

 25 is odd number
 finally block is always executed
 

Quy tắc: Đối với mỗi khối try, có thể có không hoặc nhiều khối catch, nhưng chỉ có một khối finally.

Note: Khối finally sẽ không được thực thi nếu chương trình bị thoát (bằng cách gọi System.exit() hoặc xảy ra một lỗi không thể tránh khiến chương trình bị chết).

Nguồn tin: viettuts