Java learning summary: 18

Recognize anomalies

An exception is an instruction flow in a program that causes the program to be interrupted.
Example: An exception occurs

package com.study.Demo;

public class Test1 {
    public static void main(String args[]){
        System.out.println("1.除法计算开始");
        System.out.println("2.除法计算:"+(10/0));	//此处产生异常
        //产生异常并且没有正确处理之后,异常之后的语句将不再执行
        System.out.println("3.除法计算结束");
    }
}
//结果
//1.除法计算开始
//Exception in thread "main" java.lang.ArithmeticException: / by zero
//	at com.study.Demo.Test1.main(Test1.java:6)

In order to allow the program to execute normally after an exception occurs, an exception handling statement must be introduced to improve the code writing.

Handle exceptions

Java provides three core keywords for exception handling: try, catch, and finally.

format:

try{
//有可能出现异常的语句
}[catch(异常类型 对象){
	//异常处理;
}catch(异常类型 对象){
	//异常处理;
}catch(异常类型 对象){
	//异常处理;
}...][finally{
	;不管是否出现异常,都统一执行的代码
}]

Note: The finally block can be omitted. If you omit the finally block and do not write, the program will continue to execute after the catch () block has finished running. Insert picture description here
Example: Application exception handling format

package com.study.Demo;

public class Test2 {
    public static void main(String args[]){
        System.out.println("1.除法计算开始");
        try {
            System.out.println("2.除法计算:"+(10/0));
            //异常产生之后的语句将不再执行,此处在try中产生异常,所以下面的输出不会执行
            System.out.println("666");
        }catch (ArithmeticException e){	//处理算术异常
            System.out.println("***出现异常***");
        }
        System.out.println("3.除法计算结束");
    }
}
//结果
//1.除法计算开始
//***出现异常***
//3.除法计算结束

Example: output complete information of abnormality

package com.study.Demo;

public class Test2 {
    public static void main(String args[]){
        System.out.println("1.除法计算开始");
        try {
            System.out.println("2.除法计算:"+(10/0));   //此处产生异常
            //异常产生之后的语句将不再执行,此处在try中产生异常,所以下面的输出不会执行
            System.out.println("666");
        }catch (ArithmeticException e){ //处理算术异常
            e.printStackTrace();    //输出异常的完整信息
        }
        System.out.println("3.除法计算结束");
    }
}
//结果
//1.除法计算开始
//3.除法计算结束
//java.lang.ArithmeticException: / by zero
//	at com.study.Demo.Test2.main(Test2.java:7)

Example: Use finally

package com.study.Demo;

public class Test2 {
    public static void main(String args[]){
        System.out.println("1.除法计算开始");
        try {
            int x=Integer.parseInt(args[0]);    //接收参数并且转型
            int y=Integer.parseInt(args[1]);    //接收参数并且转型
            System.out.println("2.除法计算:"+(x/y));   //此处产生异常
            //异常产生之后的语句将不再执行,此处在try中产生异常,所以下面的输出不会执行
            System.out.println("666");
        }catch (ArithmeticException e){ //处理算术异常
            e.printStackTrace();    //输出异常的完整信息
        }finally {
            System.out.println("不管是否出现异常我都执行!");
        }
        System.out.println("3.除法计算结束");
    }
}
//结果
//1.除法计算开始
//不管是否出现异常我都执行!
//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: //Index 0 out of bounds for length 0
//	at com.study.Demo.Test2.main(Test2.java:7)

Since the above program only handles arithmetic exceptions (catch (ArithmeticException e)), when other exceptions occur, the program still cannot handle them and will interrupt execution.

Example: Add multiple catches for exception handling

package com.study.Demo;

public class Test2 {
    public static void main(String args[]){
        System.out.println("1.除法计算开始");
        try {
            int x=Integer.parseInt(args[0]);    //接收参数并且转型
            int y=Integer.parseInt(args[1]);    //接收参数并且转型
            System.out.println("2.除法计算:"+(x/y));   //此处产生异常
            //异常产生之后的语句将不再执行,此处在try中产生异常,所以下面的输出不会执行
            System.out.println("666");
        }catch (ArithmeticException e){ //处理算术异常
            e.printStackTrace();    //输出异常的完整信息
        }catch (NumberFormatException e){
            e.printStackTrace();
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
        } finally {
            System.out.println("不管是否出现异常我都执行!");
        }
        System.out.println("3.除法计算结束");
    }
}
//结果
//1.除法计算开始
//不管是否出现异常我都执行!
//3.除法计算结束
//java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
//	at com.study.Demo.Test2.main(Test2.java:7)

Abnormal processing flow

Insert picture description here
Through these two exception classes, you can find that the highest inherited class of all exception types is Throwable, and there are two subclasses under Throwable.
Error: refers to the JVM error, the program is not executed at this time and cannot be handled;
Exception: refers to the exception generated during the program operation, the user can use the exception handling format to handle.

Insert picture description here
Example: Use Exception to handle exceptions

package com.study.Demo;

public class Test2 {
    public static void main(String args[]){
        System.out.println("1.除法计算开始");
        try {
            int x=Integer.parseInt(args[0]);    //接收参数并且转型
            int y=Integer.parseInt(args[1]);    //接收参数并且转型
            System.out.println("2.除法计算:"+(x/y));   //此处产生异常
            //异常产生之后的语句将不再执行,此处在try中产生异常,所以下面的输出不会执行
            System.out.println("666");
        }catch (Exception e) {   //处理所有的异常类型
            e.printStackTrace();
        } finally {
            System.out.println("不管是否出现异常我都执行!");
        }
        System.out.println("3.除法计算结束");
    }
}
//结果
//1.除法计算开始
//不管是否出现异常我都执行!
//3.除法计算结束
//java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
//	at com.study.Demo.Test2.main(Test2.java:7)

The exception of the above program is handled by Exception, so that no matter what kind of exception problem occurs in the program, the program can be caught and processed.

Note: When handling multiple exceptions, exceptions with a small capture range should be handled before exceptions with a large capture range.
According to the inheritance relationship, ArithmeticException must be a subclass of Exception, so when writing exception handling, Exception processing must be written after ArithmeticException processing, otherwise there will be a syntax error.
Example: wrong exception catching order

package com.study.Demo;

public class Test2 {
    public static void main(String args[]){
        System.out.println("1.除法计算开始");
        try {
            int x=Integer.parseInt(args[0]);    //接收参数并且转型
            int y=Integer.parseInt(args[1]);    //接收参数并且转型
            System.out.println("2.除法计算:"+(x/y));   //此处产生异常
            //异常产生之后的语句将不再执行,此处在try中产生异常,所以下面的输出不会执行
            System.out.println("666");
        }catch (Exception e){   //已经处理完所有的异常类型
            e.printStackTrace();
        }
        catch (ArithmeticException e){ //此处无法处理,Exception已处理完
            e.printStackTrace();    //输出异常的完整信息
        } finally {
            System.out.println("不管是否出现异常我都执行!");
        }
        System.out.println("3.除法计算结束");
    }
}
//结果
//Error:(15, 9) java: 已捕获到异常错误java.lang.ArithmeticException
49 original articles published · Liked25 · Visits1529

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/104394479