Errors in java should not be captured

Source: Internet
Author: User

When writing a java program, exceptions are usually caught, and some exceptions do not need to be captured forcibly. This is a bad topic. People who transfer from other languages like me are indeed a little confused. Let me explain it again with my understanding. The Exception base class is Exception, and the Exception subclass has RuntimeException and other exceptions. These other exceptions are called Checked exceptions, while RuntimeException is called Unchecked exceptions. Java prompts developers to capture known exceptions for stable running of programs. The compiler knows the exceptions that all types or methods may throw. When you use a type or method, the compiler will prompt you to catch known exceptions. The possible exceptions known by these compilers are Checked exceptions. For example, when you close a file stream, the IOException is already indicated in the close method, so the compiler prompts you to capture exceptions. However, the RuntimeException exception is unknown in the compilation stage and can be determined only in the running stage. For example, an ArithmeticException is reported when 3/0 (3 divided by 0. Because the divisor can be changed in the running stage, no capture is prompted. These runtimeexceptions are Unchecked exceptions. In short, java is to make the program as stable as possible. If you know it, you will be prompted. If you do not know it, you will be powerless. The explanation should be clear. Next we will go to the topic. Some friends may have encountered this situation when debugging the program. The program clearly encountered exceptions and caught (Exception e), but did not capture any information. There are only two reasons. 1. The thread where the Exception is located is not the same as the thread you captured. 2. The program throws an Exception but an Error. Errors, like exceptions, are inherited from Throwable, which is a serious Error that should not be captured. When I saw this explanation, I was so stupid that I didn't want to understand why I shouldn't capture the Error. If an Error occurs, the program cannot run directly. Therefore, it makes no sense to capture it. Then my problem comes again. If it is not captured, the program exits due to a problem and the log is invisible. What should I do? In fact, this assumption is not true, because if an Error exists, you will already find the problem in the development environment, and it is impossible to publish it to the official environment. Alas, it's such a silly thing to do around a large circle, so don't discuss whether Error should be captured again! I am not very knowledgeable, and I want to write a document for your guidance. It would be great if the article helped you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.