The definition of runtimeexception in the Java document is:
Runtimeexception
Is the superclass that may throw exceptions during the normal operation of the Java Virtual Machine.
Thrown but not captured during method executionRuntimeexception
No subclassThrows
Clause.
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