Exception class,
I. Exceptions
1. Exception Overview
Exception Overview
An exception is an error that occurs when the Java program is running.
2. Abnormal Inheritance System
Throwable
Error
Exception
RuntimeException
Ii. Differences between exceptions during compilation and Runtime
1. Differences between exceptions during compilation and Runtime
Exceptions in Java are classified into two categories: compile-time exceptions and runtime exceptions.
All RuntimeException classes and their subclass instances are called runtime exceptions. Other exceptions are compilation exceptions.
Exception during compilation
The Java program must be displayed for processing; otherwise, an error occurs in the program and compilation fails.
Runtime exception
No display processing is required, and the exception can be handled in the same way as during compilation.
Iii. Several Common Throwable Methods
A: getMessage ()
Obtains the exception information and returns a string.
B: toString ()
Obtains the exception class name and exception information, and returns a string.
C: printStackTrace ()
Obtain the exception class name and exception information, and the location where the exception occurs in the program. Return Value void.
Iv. Difference between throws and throw
Throws
Used after the method declaration, with the exception class name
Can be separated with multiple exception class names by commas
Indicates that an exception is thrown and is handled by the caller of this method.
Throw
Used in the method body with the exception Object Name
Only one exception object name can be thrown
Throws an exception and is processed by the statement in the method body.
V. finally features
A: finally features
The finally controlled statement body "must" be executed (even if there are return statements in try and catch)
Special case: the jvm exits before it is executed to finally (for example, System. exit (0 ))
B: Role of finally
This API is used to release resources and will be seen in IO stream operations and database operations.
C: A return Statement exists in catch, and the finally statement is also executed. Because finally is generally used to close resources, code in the finally block must be executed before return, except in special cases.