zoukankan      html  css  js  c++  java
  • 44.java异常处理之Throwable类_多个异常

    Throwable类

    1. toString() 输出该异常的类名。
    2. getMessage() 输出异常的信息,需要通过构造方法传入异常信息(例如病态信息)。
    3. printStackTrace() 打印栈信息。

    人生病:流鼻涕,感冒,呼吸道感染,肺炎。。。最后体现的是肺炎。

       医生要处理需要获知这些信息。从外到里处理。最后找病源

    /*
     Throwable类
    
     printStackTrace() 打印栈信息
    
     肺炎
     上呼吸道感染
     发烧
     流鼻涕感冒
     小感冒    
     */
    class Demo6 {
    
        public static void main(String[] args) {
    
            // Throwable able=new Throwable();
            Throwable able = new Throwable("想吐。。。");
            System.out.println(able.toString()); // 输出该异常的类名
            System.out.println(able.getMessage()); // 输出异常的信息
            able.printStackTrace(); // 打印栈信息
        }
    }

    疑问:  出现异常如何处理?

    1.   自行处理

    1. try{//可能发生异常的代码 }catch(异常类 变量名){//处理}。
    2. 案例除法运算的异常处理。
    3. 如果没有进行try catch处理,出现异常程序就停止。进行处理后,程序会继续执行。

                   

    package it.basic.javase.grammar.basicException;
    /**
     * 除数为零异常
     * */
    public class DivisorIsZero {
        public static void main(String[] args) {
            div(2, 0);
            System.out.println("main is over");
        }
    
        /*除数为零异常展示
         * */
        public static void div(int x, int y) {
    
            try {
                System.out.println(x / y); // 可能出现异常的语句,放入try中。
                System.out.println("try is over");
            } catch (ArithmeticException e) { // 进行异常匹配,
                //异常信息
                System.out.println(e.toString());
                System.out.println(e.getMessage());
                e.printStackTrace();
                System.out.println("除数不能为0");
            }
            System.out.println("除法运算");
        }
    
    }

    结果:

    java.lang.ArithmeticException: / by zero
    / by zero
    除数不能为0
    除法运算
    main is over
    java.lang.ArithmeticException: / by zero
        at it.basic.javase.grammar.basicException.DivisorIsZero.div(DivisorIsZero.java:16)
        at it.basic.javase.grammar.basicException.DivisorIsZero.main(DivisorIsZero.java:7)

    结论:

    采用try{}catch{}时,当try中代码出现异常时,try出现异常之后的代码不会执行,会直接执行catch中代码,外边程序还是会照常运行,不会中止。

    多个异常

    1. 案例print方法,形参中增加数组。
    2. 在print方法中操作数组会发生新的异常

    ArrayIndexOutOfBoundsException,NullPointerException),如何处理?

    1. 使用将可能发生异常的代码放入try语句中,添加多个catch语句即可。
    2. 可以处理多种异常,但是同时只能处理一种异常。
    3. try中除0异常和数组角标越界同时出现,只会处理一种。
    public class MoreThanOneException {
        public static void main(String[] args) {
    
            System.out.println();
            int[] arr = { 1, 2 };
            arr = null;
    
            // print (1, 0, arr);
            print (1, 2, arr);
    
            System.out.println("over");
        }
    
        public static void print(int x, int y, int[] arr) {
    
            try {
                System.out.println(arr[1]);
                System.out.println(x / y);
            } catch (ArithmeticException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("算术异常。。。");
            } catch (ArrayIndexOutOfBoundsException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("数组角标越界。。。");
            } catch (NullPointerException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("空指针异常。。。");
            }
            System.out.println("函数执行完毕");
        }
    
    }

    总结

    1. 程序中有多个语句可能发生异常,可以都放在try语句中。并匹配对个catch语句处理。
    2. 如果异常被catch匹配上,接着执行try{}catch(){} 后的语句。没有匹配上程序停止。
    3. try中多个异常同时出现,只会处理第一条出现异常的一句,剩余的异常不再处理。
    4. 使用多态机制处理异常。
      1. 程序中多态语句出现不同异常,出现了多个catch语句。简化处理(相当于急诊)。
      2. 使用多态,使用这些异常的父类进行接收。(父类引用接收子类对象)

         

    public static void div(int x, int y, int[] arr, Father f) {
    
            try {
                System.out.println(arr[1]); // 数组越界
                System.out.println(x / y); // 除零
                Son s = (Son) f; // 类型转换
    
            } catch (Exception e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("出错啦");
            }
            System.out.println("函数执行完毕");
        }

    多个catch语句之间的执行顺序。

    1. 是进行顺序执行,从上到下。
    2. 如果多个catch 内的异常有子父类关系。
      1. 子类异常在上,父类在最下。编译通过运行没有问题
      2. 父类异常在上,子类在下,编译不通过。(因为父类可以将子类的异常处理,子类的catch处理不到)。
      3. 多个异常要按照子类和父类顺序进行catch
    class Father {
    
    }
    
    class Son extends Father {
    
    }
    
    public class Demo8 {
    
        public static void main(String[] args) {
    
            System.out.println();
            int[] arr = { 1, 2 };
            arr = null;
            Father f = new Father();
            div(1, 0, arr, f);
            
            System.out.println("over");
        }
    
        public static void div(int x, int y, int[] arr, Father f) {
    
            try {
                System.out.println(arr[1]); 
                System.out.println(x / y);
                Son s = (Son) f;
    
            } catch (ArithmeticException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("算术异常。。。");
            } catch (ArrayIndexOutOfBoundsException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("数组角标越界。。。");
            } catch (NullPointerException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("空指针异常。。。");
            } catch (Exception e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("出错啦");
            }
            System.out.println("函数执行完毕");
        }
    }

    总结

    处理异常应该catch异常具体的子类,可以处理的更具体,不要为了简化代码使用异常的父类。

    疑惑:感觉异常没有作用.

     

    author@nohert
  • 相关阅读:
    Perl语言入门笔记 第十六章 进程管理
    Perl语言入门笔记 第十五章 智能匹配与given-when结构
    Perl语言入门笔记 第十四章 字符串与排序
    Perl语言入门笔记 第十三章 目录操作
    Perl语言入门笔记 第十二章 文件测试
    artdialog
    trimend("asdas".tocharArry())
    jQuery.isEmptyObject() 函数详解 转
    ajax返回数据成功 却进入error方法
    为什么有些网站PING不通但又能访问.
  • 原文地址:https://www.cnblogs.com/gzgBlog/p/13588899.html
Copyright © 2011-2022 走看看