Exception mechanism
Is the exception event that occurs during the execution of the program, which causes the program to break.
Methods to solve the exception: use try,catch,finally,throw,throws five keywords to solve;
1.try...catch ... √
2.try...catch...catch ... √
3.try...catch...finally ... √
4.try...finally ... √
5.catch...finally ... Wrong wording
The statements in the finally block are always executed, often writing the code released by the resource, and a return will be executed, unless it encounters system.exit (1), which is not executed until it exits the JVM;
There is a return in the try-catch-fianlly, the order of execution: There is no exception match in the try block to the time, Try-finally-return; there is an exception match to Try-catch-finally-return, Anyway, finally is always executed before return.
Throws and throw keyword, is in need to throw an exception to the outside, only use, generally used together;
The difference between the two:
1. Declaration location is different: throws is the exception type that declares the method when declaring a method
Throw is an exception thrown inside a method
2. The following is not the same as: throws followed by the exception type, throw followed by, new exception object;
3. Different functions: Throws is to declare the possible exception type of this method; throw is to throw out the exception that occurs, and the external call needs to handle the exception.
Throwable is the parent class for error (Errors) and exception (exceptions): All under the Java.lang package
Exception is divided into:
Run-time Exception: Exception that occurs after run, do not force try: Catch captures, common are: nullpointexception, classcastexception, NumberFormatException, Arrayindexoutofbandexception, Inputmismatchexception, arithmetic exception
Check for exceptions: Must be caught or the compilation will not pass. Common are: sqlexception,ioexception,classnotfoundexception, etc.
Custom exception: The exception in the JDK does not meet the coding requirements.
Steps:
1. Customize an exception class, inheriting exception or runtimeexception;
2. In the custom exception class, create a constructor method that inherits the parent class super (String message)
3. Create a custom exception object where an exception may occur
4. Throw an exception with throw
Java Chapter Sixth: exception mechanism