java basic notes - exception

Five anomalies

​ There is no perfect programmer or perfect machine. If it breaks down and doesn't write exceptions, it will exit directly with error

​ There are two types of exceptions:

​ > Error: stackOverflowError (stack overflow, no exit set), OOM (OutOfMemory)

​ > Exception programming reasons, accidental factors, the problem is not big, and it can be saved

​ Null pointer exception

​ The file could not be read

​ Network connection interrupted

​ Array cross table out of bounds (distinguish between and null pointers)

1. Anomaly classification

java.lang.Throwable
	|------java.lang.Error:Generally, targeted code processing is not written
	|------java.lang.Exception:You can handle exceptions
		|------Compile time exception( checked)
			|------IOXeception
				|------FileNotFoundException
			|------ClassNotFoundException
		|------RuntimeException:Runtime exception( unchecked)
			|------NullPointerException
			|------ArrayIndexOutOfBoundsExecption
			|------ClassCastExcption Type conversion exception
			|------NumberFormatException
			|------InputMismatchException Input exceptions, such as nextInt A string
			|------ArithmaticException Arithmetic operation exception

2. Exception handling:

​ try-catch-finally

​ If something happens, I'll put it in try. If something happens, I'll catch it

​ throws

​ I can't solve it. I report it (to the caller) until one can be solved. It can't be solved by reporting it to main. Let's finish it together

​ Practice:

​ > Run in try. When an exception is encountered, the jvm will generate an object corresponding to the exception and throw the object, and the subsequent code will not run

​ > Handling method of catch exception: try catch finally or throws

​ Use try catch to handle compile time exceptions so that they do not report errors, but errors may still be reported at run time, which is equivalent to delaying compile time exceptions into run-time exceptions

​ Run time exceptions are common and are generally not handled,

2.1 try-catch-finally

If the declared exception has a child parent relationship, the child class must be placed first

Common exception handling methods

​ >System.out.pringln(e.getMessage());

​ >e.printStackTrace();

try{

}catch(Exception type 1 object name 1){
    //How to handle exceptions
}catch(Exception type 2 object name 2){
    //Here's your chance. You have to take it
}finally{
	//Code that must execute
    For example, some physical connections: socket,io Stream, database connection, etc. need manual connection(prevent catch There are still anomalies in the)
}

2.2 throws

If you let main throw it, it will be thrown to the virtual machine and hang up if it can't be solved

public void method() throws FileNotFoundException,IOException{
	File file =new File("12.txt");
	FileInputStream  fis =  new FileInputStream(file);
	int data= fis.read();
	while(data!= -1){
		sout((char)data);
		data= fis.read();
	}
	fis.close();
} 
public void method2()throws FileNotFoundException,IOException {
    method1();
}
main()throws FileNotFoundException,IOException {//Mode 1
    try{    
    	method2();
    }catch (IOException e){
        e.printStackTrace();
    }
}

2.3 exceptions involved in rewriting methods

​ The exception type thrown by the method overridden by the subclass is no greater than that thrown by the method overridden by the parent class

​ Because polymorphism

​ If throws is not used for the parent class, the child class cannot be used, which means that the child class can only use trycatch

2.4 throw exception manually

​ Previously, exceptions were automatically generated by the system.

​ You can manually generate an exception object and throw it

class Student{
	private int id;
    public void regist(int id){
        if(id>0){
            thid.id=id;
        }else{
            //Throw exception manually
            throw new RuntimeException("Input data error");
        }
    }
}

2.5 customize an exception class

​ 1. Inherit an exception, runtimeException (without explicit processing), exception (to be processed)

​ 2. Provide a global constant serialVersionUID

​ 3. Provide the construction method of heavy load

class MyException extends RunTimeException{
    static final long serialVersionUID = -793489746458756785678L//Unique identification number of the serialized
    public MyException(){
    }   
    public MyException(String msg){
        super(msg);
    }
        
}

Keywords: Java

Added by Billbonic on Sun, 28 Nov 2021 11:11:09 +0200