คำแนะนำเกี่ยวกับ Java ในที่สุด Keyword

1. ภาพรวม

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

นอกจากนี้เราจะพูดถึงข้อผิดพลาดทั่วไปบางประการซึ่งสุดท้ายการบล็อกอาจมีผลลัพธ์ที่ไม่คาดคิด

2. สุดท้ายคืออะไร?

ในที่สุดก็กำหนดบล็อกของรหัสที่เราใช้ร่วมกับคำหลักลอง กำหนดรหัสที่จะรันหลังจากการลองและการดักจับทุกครั้งก่อนที่วิธีการจะเสร็จสมบูรณ์

ที่สุดรันบล็อกไม่คำนึงถึงว่ายกเว้นจะโยนหรือจับ

2.1. ตัวอย่างด่วน

ดู Let 's ที่ที่สุดในลองจับในที่สุดบล็อก:

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

ในตัวอย่างนี้โดยไม่คำนึงถึงความคุ้มค่าของพารามิเตอร์นับที่ JVM ดำเนินการในที่สุดบล็อกและพิมพ์“ในที่สุด”

2.2. ใช้ในที่สุดโดยไม่ต้องจับบล็อก

นอกจากนี้เรายังสามารถใช้ที่สุดป้องกันด้วยลองบล็อกไม่คำนึงว่าจับบล็อกเป็นปัจจุบัน :

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

และเราจะได้ผลลัพธ์:

Inside try Inside finally

2.3. ทำไมในที่สุดจึงมีประโยชน์

โดยทั่วไปเราจะใช้บล็อกสุดท้ายเพื่อดำเนินการล้างโค้ดเช่นการปิดการเชื่อมต่อการปิดไฟล์หรือการคลายเธรดเนื่องจากจะดำเนินการโดยไม่คำนึงถึงข้อยกเว้น

หมายเหตุ: สามารถใช้ try-with-resources เพื่อปิดทรัพยากรแทนการบล็อกในที่สุด

3. เมื่อถูกดำเนินการในที่สุด

มาดูการเรียงสับเปลี่ยนทั้งหมดของเวลาที่ JVM ดำเนินการบล็อกในที่สุดเพื่อให้เราเข้าใจได้ดีขึ้น

3.1. ไม่มีข้อยกเว้นถูกโยน

เมื่อลองบล็อกเสร็จสมบูรณ์ในที่สุดบล็อกจะถูกดำเนินการแม้ว่าจะมีก็ไม่มีข้อยกเว้น:

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

ในตัวอย่างนี้เราจะไม่ทิ้งข้อยกเว้นจากการบล็อกลอง ดังนั้น JVM จึงรันโค้ดทั้งหมดทั้งในการลองและบล็อกสุดท้าย

ผลลัพธ์นี้:

Inside try Inside finally

3.2. ข้อยกเว้นถูกโยนและไม่ได้รับการจัดการ

หากมีข้อยกเว้นและไม่ถูกจับสุดท้ายการบล็อกจะยังคงดำเนินการ:

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

JVM ดำเนินการบล็อกในที่สุดแม้ในกรณีที่มีข้อยกเว้นที่ไม่สามารถจัดการได้

และผลลัพธ์จะเป็น:

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

3.3. ข้อยกเว้นถูกโยนและจัดการ

หากมีข้อยกเว้นและถูกจับโดยบล็อกจับสุดท้ายบล็อกจะยังคงดำเนินการ:

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

ในกรณีนี้จับบล็อกจัดการยกเว้นโยนแล้ว JVM ดำเนินการในที่สุดบล็อกและผลิตเอาท์พุท:

Inside try Inside catch Inside finally

3.4. วิธีกลับมาจากการลองบล็อก

แม้การย้อนกลับจากวิธีการนี้จะไม่ป้องกันไม่ให้บล็อกทำงานในที่สุด :

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

ที่นี่แม้ว่าเมธอดจะมีคำสั่งreturnแต่ JVM จะดำเนินการบล็อกในที่สุดก่อนที่จะส่งการควบคุมไปยังวิธีการโทร

เราจะได้ผลลัพธ์:

Inside try Inside finally

3.5. วิธีกลับมาจากการจับบล็อก

เมื่อบล็อกจับมีคำสั่งส่งคืนสุดท้ายบล็อกจะยังคงเรียกว่า:

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"); }

เมื่อเราโยนข้อยกเว้นจากบล็อกลองบล็อกจับจะจัดการกับข้อยกเว้น แม้ว่าจะมีคำสั่งส่งคืนในบล็อกcatch JVM จะดำเนินการบล็อกในที่สุดก่อนที่จะส่งการควบคุมไปยังวิธีการโทรและส่งออก:

Inside try Inside catch Inside finally

4. เมื่อไม่มีการดำเนินการในที่สุด

แม้ว่าเรามักจะคาดหวังว่า JVM จะดำเนินการคำสั่งภายในบล็อกสุดท้ายแต่ก็มีบางสถานการณ์ที่ JVM จะไม่ดำเนินการบล็อกในที่สุด

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.

แม้ว่าเราอาจไม่ได้ตั้งใจให้มีข้อยกเว้นจากการบล็อกสุดท้ายเหมือนในตัวอย่างนี้ แต่เราอาจยังคงพบปัญหานี้ อาจเกิดขึ้นได้เมื่อวิธีการล้างข้อมูลที่เราใช้ในข้อยกเว้นการโยนบล็อกในที่สุด

6. บทสรุป

ในบทความนี้เราได้กล่าวถึงสิ่งที่บล็อกทำใน Java และวิธีใช้งานในท้ายที่สุด จากนั้นเราจึงพิจารณากรณีต่างๆที่ JVM ดำเนินการและบางกรณีอาจไม่เกิดขึ้น

สุดท้ายนี้เราได้ดูข้อผิดพลาดทั่วไปที่เกี่ยวข้องกับการใช้บล็อกในที่สุด

เช่นเคยซอร์สโค้ดที่ใช้ในบทช่วยสอนนี้มีให้ใช้งานบน GitHub