Exceptions, Generics
Exceptions
Throwable
Throwable has two subclasses: Exception and Error.
Error class represents serious errors, which are generally not recommended to be caught by catch in our programs. We can only optimize Errors by optimizing the code. When an Error occurs, JVM generally chooses to terminate the thread.
Exception class has two subclasses: Checked Exception and Unchecked Exception.
Checked Exception means that the exception will be checked at compile time. If the exception is not caught by catch or not throws, it will not pass compilation. This includes IOException, ClassNotFoundException.
Unchecked Exception means that it will not be checked at compile time. It includes RuntimeException and its subclasses, such as NullPointerException, ArrayIndexOutOfBoundsException, and ClassCastException that may occur during forced type conversion.
try-catch-finally
finally is used to close input/output streams and database connections.
| |
If return is executed inside the try block, the finally block will still be executed. The value of num is modified to 30, but the returned value is still 20. This is because the return value is already determined when return is executed inside the try block.
If System.exit() is executed inside the try or catch block, the finally block will not be executed.
How to handle multiple exceptions?
- Multiple
catchblocks catch different exceptions separately.
| |
| |
- One
catchblock catches the parent class of these exceptions.
It is best to put the catch for subclass exceptions first, so that they are not uniformly treated as parent class exceptions. You can add some logging in the catch block to facilitate troubleshooting.
throw and throws
throw actively throws an exception.
| |
throws is placed in the function signature, declaring which exceptions this method may throw, allowing the caller to handle them. If the caller does not catch to handle it, it will be thrown layer by layer up the call stack until the main function.
| |
Generics
Generic Methods
| |
Generic Classes
A generic class can accept properties of various types.
| |
Generic Interfaces
When implementing this interface, specify a concrete class.
| |
It is also possible not to specify a concrete class when implementing the interface, allowing the specific type to be specified when creating an instance of the GeneratorImpl class.
| |
Bounded Types
| |
Bytecode Files
- Type Erasure: At compile time, generic type parameters are erased, usually replaced by
Object. - Bridge Methods: Subclasses may override generic methods of superclasses. To ensure that the subclass method correctly overrides the superclass method, the compiler generates a bridge method.
| |
Obviously, the output will be “Child set”. However, this actually calls the bridge method we cannot see in the subclass.
| |
- Generic Signature: Generic information of generic classes, methods, and fields is stored in bytecode in a special format, which can be obtained through reflection APIs (e.g.,
getGenericType()).