hierarchy of Exception classes
All exception classes are subclasses that inherit from the Java.lang.Exception class.
The anomalies are divided into two types: Error and exception. ERROR
Java programs usually do not catch errors. Errors generally occur in the event of a serious failure, outside the scope of Java program processing.
Error is used to indicate errors that occur in the run-time environment.
For example, JVM memory overflows. In general, programs do not recover from errors.
The error is not an exception, but a problem that is out of programmer control. is not checked at compile time, and errors are often ignored in code. Exception
The exception is divided into Run-time exceptions (runtimeexception) and non run-time exceptions.
The exception class has two primary subclasses: the IOException class and the RuntimeException class. unchecked exception (non-check exception):
Also known as Run-time exceptions, is the runtimeexception of its subclasses, such as Common NullPointerException, indexoutofboundsexception.
For Run-time exceptions, the Java compiler does not require exception capture processing or a declaration to be thrown. checked exception (check exception, compile exception):
Also known as non run-time exceptions (exceptions to Run-time exceptions are non run-time exceptions), the Java compiler forces programmers to have capture processing that cannot be simply ignored at compile time.
Non-run-time Exceptions are exceptions that are runtimeexception. such as: IOException, SqlException and so on.
The most representative check exception is a user error or an exception caused by a problem that the programmer cannot foresee. For example, an exception occurs when you open a file that does not exist. throw/throws
If a method does not catch a check exception, then the method must be declared using the throws keyword. The throws keyword is placed at the end of the method signature.
You can also use the Throw keyword to throw an exception, whether it is newly instantiated or just captured.
A method can declare multiple exceptions to be thrown, separated by commas. finally keyword
The finally keyword is used to create a block of code that executes after the try code block.
The code in the finally code block is always executed, whether or not an exception occurs.
In a finally code block, you can run a statement that has the following properties, such as cleanup types.
Finally the code block appears at the end of the catch code block. Custom Exceptions
All exceptions must be throwable subclasses.
If you want to write a check exception class, you need to inherit the Exception class.
If you want to write a Run-time exception class, then you need to inherit the RuntimeException class.
Attention
The difference between exceptions and errors: Exceptions can be handled by the program itself, and errors cannot be handled.