Exception, what is an exception? Simply speaking of exceptions is a processing mechanism. Handle errors that are not considered by the user. Because the program ape can't guarantee that a piece of code will never go wrong.
Exceptions in Java pass through 5 keyword:try, catch, throw, throws, finally.
In the middle of the Trykeyword, a block of code enclosed in curly braces (curly braces cannot be omitted) is followed by a try block.
It puts the code that might throw an exception.
The corresponding exception type and a code block after the catch to indicate that the catch block is used to handle such a 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 a try block, and the exception mechanism guarantees that the finally block is always run.
Throwkeyword is primarily used in method signatures to declare exceptions that may be thrown by the method, while throw is used to throw an actual exception, and throw can be used as a statement, throwing a detailed exception object.
Try{The program continues to run after the exception is handled by running code exception} catch (Exception e) {Exception handling section}. 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 cannot be thrown. Even if the second one is thrown, it is the first one to conquer the case.
* It is convenient to use exceptions that cannot be inferred logically.
(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{} No matter whether the exception is caught or not. Finally will go to run.
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 processed. You can also not handle exceptions.
Define the exception yourself. 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.
If A is a runtimeexception subclass exception, because the subclass exception of the runtimeexception can be handled or not handled, the compiler agrees to call the F method when the F method throws a RuntimeException subclass exception that is not processed.
(However, it is highly recommended.) All exceptions to the throws are handled, if a exception has been processed inside a method. Don't be in 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.
The advantages of exception: ①, forcing the ape to consider the security and controllability of the program.
②, facilitates code debugging.
③, separating the error handling code from the regular code.
Java Learning------Exceptions