The following is a java4android video tutorial from marschen.
Main content of this set.
1. What is an exception.
2. Exception classification.
3. Try... catch... use of finally structure.
1. Exception: an event that interrupts the normal command flow.
It is generated during the running process of the program and has no relationship with the compilation.
The program syntax is correct. Running may also cause exceptions.
2. Exception Classification
Exception classes provided by JDK.
Throwable --- | --> exception -----> --> | runtimeexception
| --> |
| --> Erorr
3. Try... catch instance.
Finally, the finally statement is executed no matter the exception occurs.
// Uncheck exceptions can be compiled without the try, catch, or catch conditions.
class Test{public static void main(String agrs[]){System.out.println(1);//uncheck exceptiontry{System.out.println(2);int i = 1 / 0 ;System.out.println(3);}catch(Exception e){e.printStackTrace();System.out.println(4);}finally{ System.out.println("finally");}System.out.println(5);}}
// Check exceptions. If try... catch... is not added, compilation fails.
class TestCheck{public static void main(String args[]){//check exceptiontry{Thread.sleep(1000);}catch(Exception e){e.printStackTrace();System.out.println(4); } finally{ System.out.println("finally");}}}
Finally is executed regardless of whether an exception occurs or not. When opening a file, we must disable it regardless of whether an error occurs.
Therefore, operations such as file close are suitable for storing in finally.
Summary:
1. programmers can't do anything about error. They can only handle exception.
2. Handling exceptions is related to the robustness of the system.
3. Use try... catch... finally to handle possible exceptions.