The purpose of this article is to introduce Java exception classification and Java anomaly checking, so that we have a preliminary understanding of the exception. The following article will analyze the source code of the exception-related.
First, Java exception classification
Throwable: In the Java language, Throwable is a superclass of all exceptions and errors, and it has two important subclasses: Exception (Exception) and error (errors), each of which contains a large number of subclasses whose instances are created in the context of the exception condition. Contains information about the exception (such as stack trace data).
error: used to indicate a serious problem that a reasonable application should not attempt to capture. Most of these errors are exception conditions. Any subclass of the error that is thrown but not caught during the execution of the method does not need to be declared in the throws clause, because these errors may be exception conditions that will never happen again.
Take the Java Virtual machine run error (VIRTUALMACHINEERROR) as an example:
Internalerror: This exception indicates some unexpected internal errors in the Java virtual machine.
OutOfMemoryError: This exception is thrown when a Java virtual machine cannot allocate an object because of a memory overflow or when no available memory is available to the garbage collector.
stackoverflowerror: This error is thrown when the application is recursive too deep and a stack overflow occurs.
unknownerror: This error is thrown when an unknown but serious exception occurs in the Java virtual machine.
Exception (Exception): It points out the conditions that a reasonable application wants to capture.
runtimeexception (Runtime exception): He is a subclass of Exception that has to be mentioned. He is a superclass of the class of exceptions thrown during the normal operation of the Java virtual machine. Any subclass of RuntimeException that is thrown but not captured during the execution of a method does not need to be declared in the throws clause . These exceptions are usually caused by a program logic error, and the program should avoid this kind of exception as much as possible from a logical point of view.
a few of the common RuntimeException subclasses:
NullPointerException: This exception is thrown when an application attempts to use null where the object is needed. This is what we usually call a null pointer exception.
classcastexception: This exception is thrown when an attempt is made to cast an object to a subclass that is not an instance.
ArithmeticException: This exception is thrown when an operating condition of an exception occurs. For example, an integer "divided by 0" throws an instance of this class.
arrayindexoutofboundexception: indicates that a sorted index (for example, sorting an array, string, or vector) is out of range when thrown. That's what we usually call arrays out of bounds.
The " no need to declare in the throws clause " appears in the description of Error and RuntimeException, which leads us to the next question: Java exception checking.
Second, Java exception check
Java exceptions (including exception and error) are divided into checked exceptions (checked exceptions) and non-checked exceptions (unchecked exceptions).
an exception that can be checked (the compiler requires exceptions to be handled): in layman's words, such exceptions are exceptions that can be fixed and, in the event of such exceptions, must be repaired in some way. In addition to RuntimeException and its subclasses, other exception classes and their subclasses belong to the exception-checking. This type of exception may occur in a program, either by capturing it with a try-catch statement or by declaring it with a throws clause, or the compilation will not pass.
non-checked exceptions (the compiler does not require forced handling of exceptions): in layman's words, this class of exceptions is an exception that cannot be fixed. Includes run-time exceptions (RuntimeException with its subclasses) and errors (error). This type of exception can occur in a program without the need to declare it in the throws clause.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Java-exception handling mechanism" Java exception classification and anomaly checking (i)