Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between Exception and Error

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What are the differences between Exception and Error? For this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more small partners who want to solve this problem find a simpler and easier way.

Exception and Error both inherit from Throwable class. In Java code, only instances that inherit from Throwable class can be thrown or caught.

Exception is an unexpected situation that can be expected during normal operation of the program and should be caught by the developer and handled accordingly.

Error refers to a situation that is unlikely to occur under normal circumstances. Most errors will cause the program to be in an abnormal and irrecoverable state, that is, hang. So the inconvenience doesn't have to be captured by the developer, because in this case it doesn't matter if you capture it.

Java exceptions can be divided into checked exceptions and unchecked exceptions. Checkable exceptions are exceptions that the compiler requires to handle, such as IOException, which must be explicitly captured in the code. Unchecked exceptions are exceptions that the compiler does not require mandatory handling, including runtime exceptions (RuntimeException and its subclasses) and errors (Errors), such as NullPointerException.

Exception handling considerations

Try not to catch generic exceptions like Exception, but rather specific exceptions.

Software engineering is a collaborative art, and we have an obligation to make our code more intuitive and clear in our daily development.

But if you use Exception for everything, other developers won't know at a glance what exception the code is actually trying to catch, and such code will catch exceptions that you might want it to throw but don't want to catch.

2. Don't "swallow" exceptions

What happens if we catch an exception, don't throw it, or don't log it? There is no information on the line except bugs, and you don't know what went wrong or why.

This can make a simple bug difficult to diagnose. And some students prefer to use e.printStackTrace() after catch, which is usually not recommended in our products. In general, there is no problem with this method, but the output of this method is a standard error stream.

For example, in a distributed system, an exception occurs but no stacktrace can be found. Therefore, it is best to input it into the log. Our products can customize a certain format and input detailed information into the log system, which is suitable for clear and efficient troubleshooting.

Do not delay handling exceptions.

For example, if you have a method, the parameter is name, and several other methods are called inside the function, in fact, your name is passed with null value, but you do not enter this method or handle this situation at the beginning of this method, but you call several other methods and then this null pointer pops up.

In this case, obviously your error stack information only needs to throw a little information to locate the error, and after entering many methods, it may be a lump of stack information.

4, only where try-catch is needed try-catch, try-catch range can be small

As long as the necessary code segments use try-catch, do not try to indiscriminately live a lump of code, because the code in try-catch will affect the JVM's optimization of the code, such as reordering.

5. Do not control program flow through exceptions

Some conditional statements that can be used to determine if/else, such as null values, do not use exceptions, exceptions must be less efficient than some conditional statements. Moreover, each Exception instantiates a snapshot of the stack, which is a relatively heavy operation, and if the number is too large, the overhead cannot be ignored.

Do not process return values in finally blocks or return directly

Returning or processing return values in finally can cause weird things to happen, such as overriding return in try or masking exceptions. Specific can go to blog what to check, here does not specifically expand to talk about.

About the difference between Exception and Error what questions are shared here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report