Java基础-try-catch-finally处理

package com.hspedu.exception_;

public class Course449 {
    public static void main(String[] args) {
        // try-catch-finally处理细节

        /*
         * try-catch和throws处理机制:
         * 1、try-catch-finally处理机制
         *   try {
         *       可能有异常的代码
         *   } catch(Exception e) {
         *       捕获到异常
         *        1.异常发生时,系统将异常封装成Exception对象e
         *        2.将异常对象e传递给catch块
         *        3.得到异常对象进行处理
         *       如果没有异常catch代码块不会执行
         *   } finally {
         *       不管try是否有异常发生,finally一定会执行
         *       通常将释放资源的处理放在finally
         *   }
         *
         *  没有finally代码块也是可以的
         * */

        /*
         * 2、throws处理机制
         *   例如jvm调用main,main方法调用f1方法,f1方法调用f2方法
         *   当f2发生异常时,f2如果throws抛出异常,则将异常交给f1方法处理
         *   f1方法可以选择try-catch处理、也可以throws抛出异常
         *   如果f1抛出则交给main方法处理异常,如果再抛出则由jvm处理
         *   jvm处理异常:1、输出异常信息,2、中断程序
         *
         *  try-catch-finally和throws二者选一处理异常
         *  如果没有显示使用try-catch和throws,则默认使用throws抛出异常
         *
         * */

        /*
         * 1、如果发生异常,则异常后的代码不再执行,直接进入catch
         * 2、如果没有发生异常,则顺序执行try代码块,不会执行catch
         * 3、不管是否有发生异常,都会执行finally
         * */
        try {
            String str = "韩顺平教育";
            int a = Integer.parseInt(str);
            System.out.println("a = " + a);
        } catch (NumberFormatException e) {
            System.out.println("异常信息:" + e.getMessage());
        } finally {
            System.out.println("finally");
        }

        System.out.println("=================");

        try {
            String str = "123";
            int a = Integer.parseInt(str);
            System.out.println("a = " + a);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally");
        }

        System.out.println("=================");

        /*
         * 4、如果代码块可能有多个异常,可以使用多个catch分别捕获不同的异常
         *   做出相应的处理,子类的异常写在前面,父类的异常写在后面,最多只会执行一个catch
         * */
        try {
            Person person = new Person();
            person = null;
            System.out.println(person.getName());   // 空指针
            int n1 = 10;
            int n2 = 0;
            int res = n1 / n2;  // math
        } catch (NullPointerException e) {
            System.out.println("空指针异常:" + e.getMessage());
        } catch (ArithmeticException e) {
            System.out.println("算数异常:" + e.getMessage());
        } catch (Exception e) {
            System.out.println("异常信息:" + e.getMessage());
        } finally {
            System.out.println("finally");
        }

        System.out.println("=================");

        /*
         * 5、try-finally,没有捕获异常,执行完finally会退出程序,不会再执行后面的代码
         *  应用场景是执行一段代码,必须执行某个业务逻辑
         * */

        try {
            int n1 = 10;
            int n2 = 0;
//            int n2 = 2;
            System.out.println(n1 / n2);
        } finally {
            System.out.println("finally");
        }
        System.out.println("程序继续...");
    }
}

class Person {
    private String name = "jack";

    public String getName() {
        return name;
    }
}

 

package com.hspedu.exception_;

public class Course450 {
    public static void main(String[] args) {
        System.out.println(method());
    }

    public static int method() {
        try {
            String[] names = new String[3];
            if (names[1].equals("tom")) {   // NullPointerException
                System.out.println(names[1]);
            } else {
                names[3] = "hspedu";
            }
            return 1;
        } catch (ArrayIndexOutOfBoundsException e) {
            return 2;
        } catch (NullPointerException e) {
            return 3;
        } finally { // 必须执行
            return 4;
        }
    }
}

 

package com.hspedu.exception_;

public class Course450_2 {
    public static void main(String[] args) {
        System.out.println(method());   // 4
    }

    public static int method() {
        int i = 1;
        try {
            i++;    // 2
            String[] names = new String[3];
            if (names[1].equals("tom")) {
                System.out.println(names[1]);
            } else {
                names[3] = "hspedu";
            }
            return 1;
        } catch (ArrayIndexOutOfBoundsException e) {
            return 2;
        } catch (NullPointerException e) {
            return ++i; // 3
        } finally {
            return ++i; // 4
        }
    }
}

 

package com.hspedu.exception_;

public class Course450_3 {
    public static void main(String[] args) {
        System.out.println(method());   // 3
    }

    public static int method() {
        int i = 1;
        try {
            i++;    // 2
            String[] names = new String[3];
            if (names[1].equals("tom")) {
                System.out.println(names[1]);
            } else {
                names[3] = "hspedu";
            }
            return 1;
        } catch (ArrayIndexOutOfBoundsException e) {
            return 2;
        } catch (NullPointerException e) {
            return ++i; // 3
        } finally {
            ++i;    // 4
            System.out.println("i = " + i); // 4
        }
    }
}

 

posted @ 2022-03-08 16:03  柯南同学  阅读(40)  评论(0编辑  收藏  举报