try-catch error

The meaning of try-catch:
1. Error processing, output log
2. It does not affect the execution of code outside the code block, that is, the code after the error occurs and continues to execute

The difference between the try-catch code block and the throws Exception method block
1. When an error occurs in the try-catch code block, it will not affect the execution of the code outside the try-catch block in the method, and the log can be processed and output.
2. After adding throws Exception to the method block, the code after the code with the error in the method body will not be executed when an error occurs.

public class SoWhatError {

    public static void main(String args[]) throws Exception{
        int a=1,b=2;
        System.out.println("A");//执行
            a=2/0;
        System.out.println("B");//不执行
    }
}

public class SoWhatError {

public static void main(String args[]) {
    int a=1,b=2;
    System.out.println("A");//执行
    try{
        System.out.println("B");//执行
        a=2/0;
        System.out.println("C");//不执行

    }catch(Exception e){
        e.printStackTrace();
        System.out.println("D");//执行

    }
    System.out.println("E");//执行
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325527698&siteId=291194637