Interviewer: Is it better to write try-catch inside the loop or outside? Most people will get it wrong!

question

Interviewer:

Is it better to write try-catch inside the loop or outside?

I believe that most people will answer this question wrong!

Where should I write it?

Many people will say that it is better to write it outside the loop, and some people say it is better to write it inside the loop. In fact, these two answers are not completely correct. Let’s analyze the disadvantages of the two.

Disadvantages of try-catch written outside the loop:

try {
    for (...){
        // 处理逻辑
    }
} catch(Exception e) {
    ...
}

If try-catch is written outside the loop, a piece of data processing is abnormal, and the loop ends, which will lead to the end of the entire task and seriously affect the system efficiency.

The disadvantages of try-catch written in the loop:

for (...){
    try {
        // 处理逻辑
    } catch(Exception e) {
        ...
    }
}

Because exception handling will affect efficiency, if try-catch is written in the loop, it will cause excessive and unnecessary loop exception handling, which will seriously affect system efficiency.

This point is also mentioned in Alibaba's "Java Development Manual" exception handling:

The exception processing efficiency mentioned here is indeed relatively low. If try-catch is written in the loop, it is fine if the exception does not occur or if there are few exceptions. If there are more exceptions, the system efficiency will definitely decrease. This full version of the manual can be obtained by following the public account Java technology core and replying to the keyword "manual" in the background.

Therefore, no matter where it is written, it depends on the actual situation. It is necessary to make a specific choice based on the specific business. It is not absolutely good or bad where it is written.

Application Scenario

Let me summarize the application scenarios of the two, welcome to leave a message to add!

try-catch is suitable for scenarios written outside the loop:

1) A piece of data processing is abnormal, and subsequent data processing needs to be stopped;

2) Any data processing exception cannot be accepted. For example, in the transaction method, the overall success or failure is required. At this time, if a data processing exception occurs, the data processing after the loop needs to be stopped, and all processed data in the loop needs to be rolled back;

try-catch is suitable for scenarios written in loops:

1) A piece of data is processed abnormally and cannot affect the processing of other data;

2) A small number of data processing exceptions can be accepted without affecting the overall data processing;

If it is a connection timeout exception class, if you want to write it in the loop, you can set it to force exit the loop after more than N connections timeout. On the one hand, it eliminates network fluctuations, and the service may actually fail. At this time, you can avoid too many unnecessary request timeouts, and try again after the service is restored;

Summarize

At present, there is no specification that says where it must be written. Try-catch can be written inside or outside the loop. The disadvantages and application scenarios of the two are analyzed above. We can also make choices according to actual business scenarios.

What would you like to share about this interview question? Welcome to leave a message to discuss~!

Well, that’s all for today’s sharing. Later, the stack leader will share more interesting Java technology and the latest technical information. Pay attention to the public account Java technology stack to push it as soon as possible. I have also sorted out the mainstream Java interview questions and reference answers, and reply to the keyword "interview" in the background of the public account to brush up the questions.

Finally, if you feel that my article is useful to you, move your little hands, give it to someone who is reading it, forward it, it is not easy to be original, and the postmaster needs your encouragement.

Copyright statement: This article is the original work of the public account "Java Technology Stack". Please indicate the source for reprinting and quoting the content of this article. Plagiarism and manuscript washing will all be complained of infringement, and the consequences will be at your own risk, and the right to pursue legal responsibility is reserved.

Recent hot article recommendation:

1. 1,000+ Java interview questions and answers (2022 latest version)

2. Brilliant! Java coroutines are coming. . .

3. Spring Boot 2.x tutorial, too comprehensive!

4. Don't fill the screen with explosions and explosions, try the decorator mode, this is the elegant way! !

5. The latest release of "Java Development Manual (Songshan Edition)", download quickly!

Feel good, don't forget to like + forward!

Guess you like

Origin blog.csdn.net/youanyyou/article/details/131910689