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 Error and Exception of Java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the differences between Error and Exception of Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

First of all, both the Err class and the Exception class inherit the Throwable class

Error (error) is an error in the system, which the programmer cannot change and deal with. It is an error that occurs when the program is compiled and can only be corrected by modifying the program. It generally refers to the problems related to the virtual machine, such as system crash, virtual machine error, insufficient memory space, method call stack overflow and so on. For the interruption of the application caused by this kind of error, the program itself cannot recover and prevent. If you encounter such an error, it is recommended that the program be terminated.

Exception (exception) represents an exception that the program can handle, can be caught and may be recovered. If you encounter such an exception, you should handle the exception as much as possible to make the program run again, instead of terminating the exception at will.

Exception is divided into two categories.

CheckedException: (compile-time exception) requires a capture displayed by try--catch, and CheckedException for recoverable exceptions.

UnCheckedException (RuntimeException): (runtime exceptions) do not need to be caught, and use RuntimeException for program errors (unrecoverable) exceptions.

Common RuntimeException exceptions

IllegalArgumentException: this exception indicates that an illegal or incorrect parameter was passed to the method.

IllegalStateException: abnormal information that occurs when a method is awakened at an unreasonable or incorrect time. In other words, the Java environment or Java application does not satisfy the requested operation.

NullpointerException: null pointer exception (the one I have encountered most so far)

IndexOutOfBoundsException: index out of bounds exception

Common CheckedException exceptions

The only scene we captured by try--catch in the process of writing the program was CheckedException.

The IOExecption and its subclasses in the io package are all CheckedException.

Give a simple example (look at other people's, feel very vivid, easy to understand)

Error and Exception are like the difference between water in a pool and water in a pool.

The "pool" is the external environment in which the code runs normally, if the pool crashes (system crash), or the pool water overflows (memory overflow), all of which are related to the external environment of the pool. These are the error in java.

"Water in the pool", is the normal operation of the code, water pollution, impurities, turbidity, these factors affecting water quality is Exception.

This is the end of the article on "what is the difference between Error and Exception of Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report