1. Exception classification
All exception classes in Java are derived from the Throwable class, and the Throwable class has subclass error and exception class. Where error represents an internal error for the Java Runtime System, exception indicates that the program is running abnormally. The subclass of exception is divided into two main classes: RuntimeException class and other exception class, RuntimeException represents the logic error of the program, such as: nullpointerexception; other exceptions represent errors caused by external exception conditions. such as: IOException.
Java divides exceptions into checked and unchecked exceptions, non-checked exceptions include error and runtimeexception, and checked exceptions include other exception. An unchecked exception indicates that the error is either not controlled by the programmer (error), or that the programmer should avoid it altogether (runtimeexception). Just checking for exceptions means errors thrown by external conditions, and the exception handling mechanism in Java is primarily to handle checked exceptions.
2. Overrides and exceptions
Subclasses want to overwrite a method in the parent class, and if the method does not throw an exception in the parent class, the new method in the subclass cannot throw an exception, and if the method throws an exception in the parent class, the subclass can not throw an exception, and if an exception is thrown, the method in the subclass is required to throw an exception that does not exceed the exception
3.finally clause
The finally clause indicates that the code in the finally block is executed regardless of whether an exception occurred in the try block. Finally, it is commonly used to release resources that need to be manually freed in the program, such as closing files, shutting down database connections, and so on. Cases:
Try { try {
return 1; } finally { input.close ();
return 2;} } Catch (IOException e) { e.printstacktrace ();}
It is important to note that in the example above, the try statement inside has only a finally clause, which is actually a valid and recommended way to indicate that the try statement is intended to free resources and, in many cases, the code in the finally clause may throw an exception.
Assuming that the Try statement contains a return statement, and the finally clause also contains a return statement, the return statement in finally overwrites the returned value in the Try statement.
Java Exception Handling