Panduan untuk Kata Kunci akhirnya Java

1. Ikhtisar

Dalam tutorial ini, kita akan menjelajahi kata kunci terakhir di Java. Kami akan melihat bagaimana menggunakannya bersama blok coba / tangkap dalam penanganan kesalahan. Meskipun akhirnya dimaksudkan untuk menjamin eksekusi kode, kami akan membahas situasi luar biasa di mana JVM tidak menjalankannya.

Kami juga akan membahas beberapa perangkap umum di mana blok terakhir dapat memiliki hasil yang tidak terduga.

2. Apa akhirnya?

akhirnya mendefinisikan blok kode yang kita gunakan bersama dengan kata kunci try . Ini mendefinisikan kode yang selalu dijalankan setelah percobaan dan blok catch apa pun , sebelum metode selesai.

The akhirnya blok mengeksekusi terlepas dari apakah eksepsi dilemparkan atau tertangkap .

2.1. Contoh Cepat

Mari kita lihat akhirnya di blok coba-tangkap-akhirnya :

try { System.out.println("The count is " + Integer.parseInt(count)); } catch (NumberFormatException e) { System.out.println("No count"); } finally { System.out.println("In finally"); } 

Dalam contoh ini, terlepas dari nilai jumlah parameter , JVM mengeksekusi blok akhirnya dan mencetak "Akhirnya" .

2.2. Menggunakan akhirnya Tanpa blok tangkapan

Juga, kita dapat menggunakan sebuah akhirnya memblokir dengan mencoba blok terlepas dari apakah tangkapan blok hadir :

try { System.out.println("Inside try"); } finally { System.out.println("Inside finally"); }

Dan kita akan mendapatkan hasilnya:

Inside try Inside finally

2.3. Mengapa akhirnya Berguna

Kami biasanya menggunakan blok terakhir untuk mengeksekusi kode pembersihan seperti menutup koneksi, menutup file, atau membebaskan utas, karena dijalankan terlepas dari pengecualian.

Catatan: coba-dengan-sumber daya juga dapat digunakan untuk menutup sumber daya alih-alih blok akhirnya .

3. Kapan akhirnya Dieksekusi

Mari kita lihat semua permutasi ketika JVM akhirnya mengeksekusi blok, sehingga kita bisa memahaminya dengan lebih baik.

3.1. Tidak Ada Pengecualian yang Dilempar

Ketika blok percobaan selesai, blok terakhir dijalankan, bahkan jika tidak ada pengecualian:

try { System.out.println("Inside try"); } finally { System.out.println("Inside finally"); }

Dalam contoh ini, kami tidak membuat pengecualian dari blok percobaan . Jadi, JVM mengeksekusi semua kode di blok coba dan akhirnya .

Output ini:

Inside try Inside finally

3.2. Pengecualian Dilempar dan Tidak Ditangani

Jika ada pengecualian dan tidak tertangkap, blok terakhir masih dijalankan:

try { System.out.println("Inside try"); throw new Exception(); } finally { System.out.println("Inside finally"); }

JVM mengeksekusi blok terakhir bahkan dalam kasus pengecualian yang tidak tertangani.

Dan hasilnya adalah:

Inside try Inside finally Exception in thread "main" java.lang.Exception

3.3. Pengecualian Dilempar dan Ditangani

If there's an exception and it is caught by the catch block, the finally block is still executed:

try { System.out.println("Inside try"); throw new Exception(); } catch (Exception e) { System.out.println("Inside catch"); } finally { System.out.println("Inside finally"); }

In this case, the catch block handles the thrown exception, and then the JVM executes the finally block and produces the output:

Inside try Inside catch Inside finally

3.4. Method Returns from try Block

Even returning from the method will not prevent finally blocks from running:

try { System.out.println("Inside try"); return "from try"; } finally { System.out.println("Inside finally"); }

Here, even though the method has a return statement, the JVM executes the finally block before handing the control over to the calling method.

We'll get the output:

Inside try Inside finally

3.5. Method Returns from catch Block

When the catch block contains a return statement, the finally block is still called:

try { System.out.println("Inside try"); throw new Exception(); } catch (Exception e) { System.out.println("Inside catch"); return "from catch"; } finally { System.out.println("Inside finally"); }

When we throw an exception from the try block, the catch block handles the exception. Though there is a return statement in the catch block, the JVM executes the finally block before handing control over to the calling method, and it outputs:

Inside try Inside catch Inside finally

4. When finally Isn’t Executed

Although we always expect the JVM to execute the statements inside a finally block, there are some situations where the JVM will not execute a finally block.

We might already expect that if the operating system stops our program, then the program would not get the chance to execute all of its code. There are also some actions we can take that will similarly prevent the execution of a pending finally block.

4.1. Invoking System.exit

In this case, we are terminating the JVM by calling System.exit and hence the JVM will not execute our finally block:

try { System.out.println("Inside try"); System.exit(1); } finally { System.out.println("Inside finally"); }

This outputs:

Inside try

4.2. Invoking halt

Similar to System.exit, a call to Runtime.halt also halts the execution and the JVM does not execute any finally blocks:

try { System.out.println("Inside try"); Runtime.getRuntime().halt(1); } finally { System.out.println("Inside finally"); }

Thus, the output will be:

Inside try

4.3. Daemon Thread

If a Daemon thread enters the execution of a try/finally block and all other non-daemon threads exit before the daemon thread executes the finally block, the JVM doesn’t wait for the daemon thread to finish the execution of finally block:

Runnable runnable = () -> { try { System.out.println("Inside try"); } finally { try { Thread.sleep(1000); System.out.println("Inside finally"); } catch (InterruptedException e) { e.printStackTrace(); } } }; Thread regular = new Thread(runnable); Thread daemon = new Thread(runnable); daemon.setDaemon(true); regular.start(); Thread.sleep(300); daemon.start();

In this example, the runnable prints “Inside try” as soon as it enters the method and waits for 1 second before printing “Inside finally”.

Here, we start the regular thread and the daemon thread with a small delay. When the regular thread executes the finally block, the daemon thread is still waiting within the try block. As the regular thread completes execution and exits, the JVM also exits and does not wait for the daemon thread to complete the finally block.

Here's the output:

Inside try Inside try Inside finally

4.4. JVM Reaches an Infinite Loop

Here's a try block which contains an infinite while loop:

try { System.out.println("Inside try"); while (true) { } } finally { System.out.println("Inside finally"); }

Though it's not specific to finally, it's worth mentioning that if the try or catch block contains an infinite loop, the JVM will never reach any block beyond that loop.

5. Common Pitfalls

There are some common pitfalls that we must avoid when we use the finally block.

Although it's perfectly legal, it's considered bad practice to have a return statement or throw an exception from a finally block, and we should avoid it at all costs.

5.1. Disregards Exception

A return statement in the finally block ignores an uncaught exception:

try { System.out.println("Inside try"); throw new RuntimeException(); } finally { System.out.println("Inside finally"); return "from finally"; }

In this case, the method ignores the RuntimeException thrown and returns the value “from finally”.

5.2. Ignores Other return Statements

A return statement in the finally block ignores any other return statement in the try or catch block. Only the return statement in the finally block executes:

try { System.out.println("Inside try"); return "from try"; } finally { System.out.println("Inside finally"); return "from finally"; }

In this example, the method always returns “from finally” and completely ignores the return statement in the try block. This could be a very difficult bug to spot, which is why we should avoid using return in finally blocks.

5.3. Changes What's Thrown or Returned

Also, in the case of throwing an exception from a finally block, the method disregards the exception thrown or return statements in the try and catch blocks:

try { System.out.println("Inside try"); return "from try"; } finally { throw new RuntimeException(); }

This method never returns a value and always throws a RuntimeException.

Meskipun kami mungkin tidak sengaja memberikan pengecualian dari blok terakhir seperti dalam contoh ini, kami mungkin masih mengalami masalah ini. Itu bisa terjadi ketika metode pembersihan yang kita gunakan di blok akhirnya membuat pengecualian.

6. Kesimpulan

Pada artikel ini, kita membahas apa yang akhirnya dilakukan blok di Java dan bagaimana menggunakannya. Kemudian, kami melihat berbagai kasus di mana JVM mengeksekusinya, dan beberapa jika mungkin tidak.

Terakhir, kami melihat beberapa kendala umum yang terkait dengan penggunaan blok terakhir .

Seperti biasa, kode sumber yang digunakan dalam tutorial ini tersedia di GitHub.