Learn JAVA 20th day, contact exception handling, customize exception

1.java.lang.NullPointerException (Runtime Exception) is a runtime exception that the compiler cannot predict, such as when you define a Reference variable String a, but you did not instantiate the variable with the new keyword.

There are two solutions to this null pointer exception, because you call this a's method again
if(null!=a) this variable is called first
2. try..catch catches exceptions to allow the program to continue executing

2.java.lang.ClassCastException (conversion error)

3.java.lang.ArithmeticException (operation error)

4.java.lang.ArrayIndexOutOfBoundsException (Boundary Error)

5.java.lang.ClassNotFoundException (no classes found) is usually a typing error or environment configuration problem, such as unsuccessful download of packages such as classpath or maven

6.java.lang.SQLException(SQL statement with exception)

 

First, exceptions are classified as try--catch--finally, which declares throws.

try{...} The code inside the code block is checked and exception objects are thrown.

catch{...} This handles exceptions thrown by try, and the code block writes the handling code.

finally{...} The code here is bound to be executed.Usually used to clean up and release resources

Among them, today the teacher said: Custom exception.The purpose is to have a categorical discussion.

Code:

public class Test{

    public static void main(String args[]) throws Exception{
        
        //Students'normal learning,
        //But there are various exceptions:1.Have a stomachache-->>See a doctor
        //              2.Vomit-->>Ask the students in front to take to the hospital,
        //                        Ask the classmates next door to clean up
        //              3.Can not do-->>Ask a classmate to ask a teacher
        //              4.Fell asleep-->>Consult
        //Step 1: Define different exception classes(extends Exception)
        //try catch()...
        try{
            Student s = new Student();
            s.study();//If an exception occurs here, the try snippet stops and the exception is handled by the catch.Other code will not stop
            System.out.println("learn java Learning well");
        }
        catch(Exception e1){
            System.out.println(e1.getMessage());Print the exception handled by bar for easy viewing
        }
        System.out.println("Program continued running 1");
        System.out.println("Program continued running 2");
        System.out.println("Program End");
    }
}    

 

public class Student{
    public void study() throws Have a stomachache,Vomit,Can not do,Fell asleep{//This is inherited from the reported exceptions, that is, the number of possible exceptions, and the corresponding exception class is customized below
        System.out.println("ok come in");
    }
    public void study(String i) throws Have a stomachache,Vomit,Can not do,Fell asleep{
        
        if (i.equals("Have a stomachache")){
            throw new Have a stomachache();//Corresponding exception found
        }
        if(i.equals("Vomit")){
            throw new Vomit();
        }
        if(i.equals("Can not do")){
            throw new Can not do();
        }
        if(i.equals("Fell asleep")){
            throw new Fell asleep();
        }    else{
      System.out.println("Other exceptions!!!");
    }
   }
}

The exception below is custom

public class Have a stomachache extends Exception{//Handle the corresponding exception
    private String message = "See a doctor";
    
    @Override
    public String getMessage(){
        return message;
    }
    
}

 

public class Vomit extends Exception{
    private String message = "Ask the students in front to take to the hospital,Ask the classmates next door to clean up";
    
    @Override
    public String getMessage(){
        return message;
    }
    
}

 

public class Can not do extends Exception{
    private String message = "Ask a classmate to ask a teacher";
    
    @Override
    public String getMessage(){
        return message;
    }
    
}

 

public class Fell asleep extends Exception{
    private String message = "Consult";
    
    @Override
    public String getMessage(){
        return message;
    }
    
}

 

Perhaps my language organizing ability is not very good, there are some corresponding details can not be expressed in words, and we hope to understand!!!

Scholars, there are some wrong places, but also look forward to your big guys pointing out!!!

Keywords: Java Maven SQL

Added by ypirc on Wed, 15 May 2019 13:47:55 +0300