You really try catch finally understand it? (Interview questions)

I. Description

try catch finally in the interview also often encountered, I still junior Well, within a few days ago by seniors pushed a trainee post Ali. Pre-interview yesterday, my brother was a good one, it sounds too good to hear the sound, it should be very young. Talk is also very pleasant.

Suddenly talk to "try catch finally", then I'll process by vernacular to write about it, and add some analysis.

Second, the exception is caught and thrown distinguish abnormal

try where it is possible to throw the exception;
the catch is used to capture the exception;

If a method is fully capable of handling errors that occur on its own can deal with their own, if you can not handle the error, you can throw again thrown

Third, we must first understand why you want to throw an exception?

1, in order to prevent program crashes when an exception is encountered, affecting the user.
2, throws an exception also convenient for developers to debug, so that errors can be tracked.
3, but also to locate the error in any place, when there is a problem, at least know where the problem.
4, exception handling, so you can take control of your applications according to your logic (for example: a user prompt an error, etc.).
Of course, there are many reasons. The contrary is bad.

Fourth, the skills to handle exceptions

Principles and techniques Java exception handling

1, to avoid excessive try block, the exception code does not occur inside the try block, keep a try block corresponding to one or more exceptions.
2, the type of refinement of anomaly, not matter what type of exceptions written Excetpion.
3, catch blocks try to keep a block to catch a class of abnormal abnormal, do not ignore captured, or after capture to processing, or translation, or re-throw new types of exceptions.
4, do not you can handle exceptions thrown to someone else.
5. Do not try ... catch involved in the control program flow, the fundamental purpose is to control abnormal abnormal condition handler.

发布了129 篇原创文章 · 获赞 111 · 访问量 4万+

Guess you like

Origin blog.csdn.net/qq_17623363/article/details/104730604