Exception classes and Categories in Java

Source: Internet
Author: User

Hierarchy of Exception classes:

In Java, all exceptions have a common ancestor throwable (can be thrown). Throwable specifies the commonality of any problems that the exception propagation mechanism can transmit through Java applications in the code, both under the Java.lang package.

The error class flagged a fatal error, such as a memory overflow, a virtual machine exception, and so on, which should not have occurred. These errors indicate that the failure occurred on the virtual machine itself, or when the virtual machine attempted to execute the application, such as a Java Virtual machine run error (virtual Machineerror), a class definition error (NOCLASSDEFFOUNDERROR), and so on. These errors are not available because they are outside the control and processing power of the application and are mostly not allowed when the program is running. For a well-designed application, even if it does, it should not be an attempt to deal with the anomalies that it causes. In Java, errors are described by the subclasses of the error.

The exception class has an important subclass runtimeexception. The RuntimeException class and its subclasses represent errors thrown by the JVM common operation. For example, if you attempt to use null object references, divide by zero, or array out of bounds, the run-time exceptions (NullPointerException, ArithmeticException), and arrayindexoutofboundexception are raised respectively.
This type of exception can be captured.

Note: Exceptions and errors are different: Exceptions can be handled by the program itself, and errors cannot be handled.

Check and non-check exceptions

Java exceptions (including exception and error) are categorized as checked exceptions and non-checked exceptions (unchecked exceptions).

can check exceptions (compiler requirements must be disposed of exceptions): the correct program in operation, it is easy to appear, reasonable tolerance of abnormal conditions. Although the abnormal condition can be checked, it can be predicted to some extent, and in the event of such abnormal situation, it must be handled in some way.
In addition to RuntimeException and its subclasses, other exception classes and their subclasses belong to the exception-checking. This exception is characterized by the Java compiler checking it, that is, when such an exception can 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.

Exception not checked (compiler does not require forced disposition of exceptions): Includes run-time exceptions (RuntimeException with its subclasses) and errors (error).

Running with non-running exceptions

Exception This exception is divided into two major classes of runtime exceptions and non-runtime exceptions (compilation exceptions). These exceptions should be handled as much as possible in the program.

Run-time Exceptions: both the RuntimeException class and its subclass exceptions, such as nullpointerexception (null pointer exception), indexoutofboundsexception (subscript out-of-bounds exception), and so on, these exceptions are not checked for exceptions, You can choose to capture or not handle the program. 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 run-time exception is characterized by the Java compiler not checking it, that is, when such an exception can occur in a program, even if it is not captured with the Try-catch statement, it is not thrown with the throws clause declaration, and it is compiled.

Non-runtime exception (compile exception): is an exception other than RuntimeException, and the type belongs to the exception class and its subclasses. From the point of view of the program syntax is the exception that must be handled, and if not handled, the program cannot be compiled through. such as IOException, SqlException, and user-defined exception exceptions, but generally do not customize check exceptions.

Exception classes and categories in Java

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.