Exception, what is an exception? Simply speaking of exceptions is a processing mechanism. Handle errors that are not considered by the user. Because programmers cannot guarantee that a piece of code will never go wrong.
Exceptions in Java pass through 5 keywords: try, catch, throw, throws, finally. Where the Try keyword is immediately followed by a block of code enclosed in curly braces (the curly braces must not be omitted) short as a try block. It puts the code that might throw an exception. A catch corresponds to an exception type and a code block that indicates that the catch block is used to handle this type of code block. Multiple catch blocks can be followed by a finally block, and finally blocks are used to reclaim the physical resources opened in the try block, and the exception mechanism guarantees that the finally block is always executed. The throw keyword is primarily used in method signatures to declare exceptions that the method might throw, and throw is used to throw an actual exception, which can be used as a statement, throwing a specific exception object.
try{execution Code Exception} catch (Exception e) {Exception handling section} The program continues execution after the exception is processed. catch () in parentheses for the caught exception. The exception type does not know to write exception. E.printstacktrace () is simply understood as printing exception information.
* A piece of code can not throw 2 exceptions, because the second can not be thrown, even if the second throw, but also in the first solved the case.
* Can not be used in logic to determine the use of unusual convenience. (Meaning of the exception)
There are 2 ways to handle exceptions. ①try{}catch () {} handles the exception itself. ② after the method is added throws to the Java Virtual machine processing.
Exception handling steps: try{} catch (1) {} catch (2) {} ... finally{} whether or not an exception is caught, finally will execute. Finally, all exceptions are managed uniformly. General finally is the management of mechanisms such as memory recycling.
Exception classification: ①, error system errors. ②, Exception must handle the exception. ③, runtimeexception can be handled, also can not handle the exception.
Custom exception, throws new ... and inherit exception.
void F () throws A
{
}
*throws a means that when the F method is called, the F method may throw a class A exception, and it is recommended that you catch a Class A exception that may be thrown by the F method when you call the F method.
*throws A does not mean that the F method is bound to throw a class A exception, and the F method can not throw an exception.
*throws A does not mean that a class A exception must be snapped when the F method is called. Suppose A is a runtimeexception subclass exception, because the subclass exception of RuntimeException can be handled or not handled, so the compiler allows the F method to be called when the F method throws a RuntimeException subclass exception that is not processed. (However, it is strongly recommended that all exceptions to the throws be handled, and if a method has been processed internally, do not throws a)
* Catch subclass exception and Catch parent class exception first
There can be no other code between *catch and catch.
* The override method throws an exception scope that cannot be greater than the exception range that is excluded by the overridden method.
Advantages of exceptions: ①, forcing programmers to consider the security and controllability of the program.
②, facilitates code debugging.
③, separating the error handling code from the regular code.
Java Learning------Exceptions