Home > Java > javaTutorial > body text

Why does Java need to design exceptions?

青灯夜游
Release: 2019-11-18 16:49:26
Original
2631 people have browsed it

Why does Java need to design exceptions?

After so many years of working in the industry, whenever I talk about exceptions, I am confused. I just vaguely remember that it handles errors. When a program error occurs, there will be an exception log in the log. You can check it. Exception location error. But recently I suddenly discovered a problem, that is, handling errors does not necessarily require the use of exceptions, such as parameter legality checks, etc., directly returning verification information after judging whether it is empty, etc., which can be handled by various means of the program. , so why use exceptions? What problem can it solve?

When we usually write programs, many people ignore one issue, that is "whether the program has been executed correctly", and often only care about whether it has been executed. In this way, the opportunity to really catch the error is often missed, and subsequent errors are derived, which is not the real cause of the accident. So we need an error handling mechanism.

When there was no exception handling mechanism in the early days. The error handling model often returns a special value or sets a flag, and it is assumed that the caller will check the return value or flag to determine whether an error has occurred. However, as time goes by, people find that arrogant programmers are more inclined to: "Errors may occur, but they are caused by others and have nothing to do with me." So it's not surprising that it's not checked. There are some functions that are often boring to deal with such checks, such as: println.

If you check thoroughly every time you call a method, the code will become difficult to read. And each data structure containing error information needs to be defined as the return type of the function.

●If there is no exception, then you must check for a specific error and handle it in the program, so that the code reuse rate will be reduced.

●With the exception mechanism, you can separate the code that "describes what to do during normal execution" from the code that "what to do if something goes wrong".

So the benefits of designing exceptions: it can liberate the way to judge the return value, provide a throw-up processing mechanism, reduce the judgment complexity of the code, and ensure that the exception is caught, centralized processing, and enhanced code reuse Rate.

Why does Java need to design exceptions?

The above is the detailed content of Why does Java need to design exceptions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!