Checkexception and runtimeexception

Source: Internet
Author: User
Tags stack trace

The definition of runtimeexception in the Java document is:

RuntimeexceptionIs the superclass that may throw exceptions during the normal operation of the Java Virtual Machine.

Thrown but not captured during method executionRuntimeexceptionNo subclassThrowsClause.

 

In Java, exceptions are classified into two types: checkexception and uncheckexception. And all Java errors belong to uncheckedexception.

I. Differences between checkexception and uncheckexception:

1. during compilation, the Java compiler will force you to handle checkexception. There are two ways to handle it: one is to throw an exception; the other is to capture exceptions (classnotfoundexception and so on ). For uncheckexception compilation, you do not need to perform any processing. However, if this type of exception occurs during runtime, it will be thrown (Common examples include nullpointexception and arrayindexoutofboundexception ).

2. Checked exception indicates an exception that the caller can directly handle. Runtime exception indicates a program error that the caller cannot handle or recover.

 

Ii. Whether to handle runtimeexception

All in all, when a checked exception is thrown during program running, only calls that can properly handle this exception should be captured using try/catch. For a runtime exception, it should not be captured in the program. If you want to capture it, you will take the risk that program code errors (bugs) are hidden and invisible during running. During program testing, the stack trace printed by the system often allows you to locate and modify errors in the Code more quickly. Some programmers suggest capturing the runtime exception and recording it in the log. I am opposed to this. The downside is that you must browse the log to find out the problem, but the testing system (such as unit test) used to test the program cannot directly capture the problem and report it.

Capturing a runtime exception in a program brings about more problems: Which runtime exceptions should be captured? When will it be captured? Runtime exception does not need to be declared. How do you know if runtime exception is to be captured? Do you want to see that every time a method is called in a program, the try/Catch Block is used?

Checkexception and runtimeexception

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.