Introduction: In Java program, due to the negligence of programmers and changes in environmental factors, there will often be abnormal situations. In order to ensure the normal operation of the program, the Java language specially provides an exception processing mechanism. Java defines corresponding exception classes for various common exceptions and establishes an exception class system.

Class hierarchy of exceptions:

Photo source:www.icourse163.org/learn/BFU-1…

Meaning of different types of exceptions

1. The Throwable class is the root and the parent of all exception classes. Only objects of the Throwable class and its subclasses in Java can be handled by the exception handling mechanism.

2. The main methods provided by the Throwable class include retrieving information about exceptions and displaying stack trace trace information about where exceptions occurred.

3. Exceptions handled by Java fall into two broad categories

 Error and its subclasses

 Exception and its subclasses, where Exception is divided into RuntimeException and other Exception classes

4.Error means a serious Error that is difficult to recover and is generally not handled by a program

  1. RuntimeException means a programming or implementation problem that is handled by a policy of correcting errors and reprogramming

6. Other exceptions are usually caused by environmental factors, such as file nonexistence, invalid URL, etc. Such exceptions are often caused by user misoperations and can be handled in exception handling

Photo source:www.icourse163.org/learn/BFU-1…

2. Exception handling mechanism

In Java applications, the exception handling mechanism is: throw an exception, catch an exception.

Throw exception: When a method fails to throw an exception, the method creates an exception object and delivers it to the runtime system. The exception object contains exception information such as the type of exception and the state of the program when the exception occurs. The runtime system is responsible for finding and executing the code that handles the exception.

Catch exception: After the method throws an exception, the runtime system turns to finding the appropriate exception handler. A potential exception handler is a collection of methods that remain in turn in the call stack when an exception occurs. An exception handler is a proper exception handler when the type of exception it can handle matches the type of exception thrown by the method. The run-time system starts with the method where the exception occurred and goes back to the methods in the call stack until it finds a method with an appropriate exception handler and executes it. When the runtime system traverses the call stack without finding a suitable exception handler, the runtime system terminates. Also, it means the termination of the Java program.

Java technology requires different exception handling for runtime exceptions, errors, or traceable exceptions.

Due to the untraceability of runtime exceptions, to make it more reasonable and easy to implement applications, Java specifies that runtime exceptions are automatically thrown by the Java runtime system, allowing applications to ignore runtime exceptions.

Java allows a method to not make any throw declarations for errors that may occur during a method run when the running method does not want to catch them. For the most part, Error exceptions are conditions that should never be allowed to occur and exceptions that a reasonable application should not catch.

Except for RuntimeException and its subclasses, all Exception classes and their subclasses are traceable exceptions. This type of exception is checked by the Java compiler. That is, when such an exception is possible in a program, it is either caught with a try-catch statement or thrown with a throws clause declaration, or the compilation fails.

Uncheckable exceptions (exceptions that the compiler does not mandate) : Includes runtime exceptions (runtimeExceptions and their subclasses) and errors.

Exception there are two broad categories of runtime and non-runtime exceptions (compile exceptions). Your program should handle these exceptions as much as possible.

Runtime exception: Are RuntimeException class and its subclasses abnormalities, such as NullPointerException (null pointer exception), IndexOutOfBoundsException anomaly) (subscript bounds, etc., these exceptions are not checked exception, in the program can choose to capture processing, also can not handle. These exceptions are usually caused by program logic errors, and programs should logically avoid them as much as possible.

Methods that can catch exceptions need to provide exception handlers of the appropriate type. The exception caught can be an exception thrown by its own statement, thrown by a method called, or by a Java runtime system, for example. That is, any exception that a method can catch must be an exception thrown by Java code somewhere. Simply put, exceptions are always thrown first and then caught.

Any Java code can throw an exception, such as code you wrote yourself, code from a Java development environment package, or a Java runtime system. Anyone can throw an exception through a Java throw statement.

Any exception thrown from a method must use the throws clause.

Exceptions are caught by try-catch or try-catch-finally statements.

In general, Java states that you must catch or declare a throw for a traceable exception. Allows untraceable RuntimeExceptions and errors to be ignored.