Abnormal event
-
Error: problems that cannot be solved by the java virtual machine, such as internal errors in the JVM system and resource exhaustion, which are not handled by writing code
-
Exception: problems caused by programming errors or accidental external factors can be solved with targeted code, such as null pointer access (mainly learning!)
Anomaly classification
-
Compile time exception
-
ClassNotFoundException
-
IOException
-
FileNotFoundException
-
-
-
Runtime exception: an exception that is not found until the program runs
-
NUllPointerException
-
ClassCastException
-
NumberFormatException
-
Exception handling (grab and throw model)
Grab and throw model
-
Throw: once an exception occurs, generate an object of the exception class and throw it. After throwing, the code will no longer be executed!
-
Catch: it can be understood as how to handle the thrown exception
Throws
Who wants him to throw to who, always throw up
The code after throwing is no longer executed
It just threw the exception to the caller of the method and didn't handle it
public class Throws { public static void main(String[] args) { try { method2();//It is not recommended to throw it into the main method and capture it with try catch } catch (IOException e) { e.printStackTrace(); } } public static void method2() throws IOException {//Parent class method1(); } public static void method1() throws IOException { File file=new File("hello.txt"); FileInputStream fis=new FileInputStream(file); int data=fis.read(); while(data!=-1){ System.out.println((char)data); data=fis.read(); } } }
The exception thrown by the subclass override method is not greater than that of the parent class
public class ExtendsTest { public static void main(String[] args) { ExtendsTest e=new ExtendsTest(); e.dispaly(new Subclass());//polymorphic } public void dispaly(Superclass s){ try { s.method(); } catch (IOException e) {//Exception handling. The maximum exception is IOEException. The subclass exception is larger than this exception and cannot be handled e.printStackTrace(); } } } class Superclass{ public void method() throws IOException{ } } class Subclass extends Superclass{ @Override public void method() throws IOException { super.method(); } }
Try catch finally (capable of handling)
-
format
try{
//Possible exception codes
}catch (exception type 1 variable name 1){
//How to handle exceptions 1
}catch (exception type 2 variable name 2){
//How to handle exceptions 2
}
......
finally{
//Code that must execute
}
explain:
finally, you can not write;
The exception type cannot be written wrong;
The order of exception types from child to parent cannot be reversed;
After the exception handling is completed, jump out of the try catch structure and continue to execute the subsequent code;
Processing method: getMessage method of String type; printStackTrace() method;
The variables declared by the try structure cannot be called after they are out of the structure. Variables can be defined outside the try structure
This is equivalent to delaying a compile time exception until it occurs at runtime
public class FinallyTest { public static void main(String[] args) { FileInputStream fis=null; try { File file=new File("hello.txt"); fis=new FileInputStream(file); int data=fis.read(); while(data!=-1){ System.out.println((char)data); data=fis.read(); } } catch (IOException e) { e.printStackTrace(); }finally{ try { if(fis!=null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
-
finally
finally, the statement must be executed
public static void main(String[] args) { Test test=new Test(); System.out.println(test.test());//I must be executed 3 } public int test() { try{ int[] arr=new int[10]; System.out.println(arr[10]); return 1;//It's reasonable to encounter a return and jump out of the method }catch(ArrayIndexOutOfBoundsException e){ e.printStackTrace(); return 2; }finally { System.out.println("I must be executed"); return 3; } }
Use situation
-
The method overridden by the parent class has no throws exception, and the method overridden by the child class cannot use throws. If the method overridden by the child class has exceptions, it must be handled by try catch
-
Try catch is used for centralized exception handling. The execution method is progressive, because if the first exception occurs, there is no need to go down
Throw exception manually
-
When an exception occurs in the program, the exception object is automatically generated
-
Manually generate an exception object with throw
public class StudentTest { public static void main(String[] args) { Student s=new Student(); try { s.regist(-1); } catch (Exception e) { e.printStackTrace(); } } } class Student{ private int id; public void regist(int id) throws Exception {//Handle what we threw manually if(id>0){ this.id=id; }else{ //System.out.println("illegal input data!"); //throw new RuntimeException("illegal input data"); throw a runtime exception and do not handle it throw new Exception("Illegal input data"); } } }
Custom exception
-
Inherit the existing Exception structure, RuntimeException, Exception
-
Provide global constant: serialVersionUID
-
Provides overloaded constructors
public class MyException extends Exception{ static final long serialVersionUID = -3387516993124229948L; public MyException(){ } public MyException(String s){ super(s); } }