2

"Does the finally block always execute?"

This is a problem that a friend who has worked for 3 years recently encountered when he went for an interview.

How would you answer this question?

Hello everyone, I'm Mic, a Java programmer who has been working for 14 years

What is the interviewer looking for in this question?

Problem Analysis

This question, obviously, is to examine the basics of Java.

The finally statement block is used a lot in actual development, it is used in combination with the try statement block.

Under normal circumstances, the code in the finally statement block is bound to be executed regardless of whether an exception is triggered, so we put the release of resources or the printing of business logs in the finally statement block.

Therefore, when people regard this concept as a fixed formula, they rarely think about when the finally statement block is not executed.

This is also the reason why many job seekers are stumped, so I think this question mainly examines two aspects:

  • The understanding of the finally keyword is actually to examine the Java foundation. A good Java foundation can write more stable and robust code.
  • Whether they have the spirit of exploring technology, such people will grow faster than the average person in terms of technology

Expert:

The finally block is not executed in two cases:

  • The program does not enter the try statement block because the program terminates due to an exception. This problem is mainly due to the insufficient scope of exception capture when developers write code.
  • In the try or cache statement block, the System.exit(0) statement is executed, causing the JVM to exit directly

Summarize

Well, today's sharing ends here

If you like my work, remember to like, favorite, and follow

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。