异常

7.1异常基本概念
异常是导致程序中断的运行一种指令流,所有异常均以类和对象的形式存在,可自定义异常类,处理抛出异常同时不会降低运行效率
被除数为0会导致异常

package org.lxh.debugdemo;

public class DebugDemo {
    public static void main(String args[]) {
        System.out.println("计算开始");
        int i = 10;
        int j = 0;
        try {
            String str1 = args[0];
            String str2 = args[1];
            i = Integer.parseInt(str1);
            j = Integer.parseInt(str2);
            int temp = i / j;
            System.out.println("两个数字相处结果:" + temp);
            System.out.println("--------------------------");
        } catch (ArithmeticException e) {
            System.out.println("出现异常:" + e);
        }catch (NumberFormatException e) {
            System.out.println("数字转换异常:" + e);
        }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界异常:"+e);
        }
        System.out.println("计算结束");
    }
}

7.1.3异常类的继承结构
Exception类(程序中出现的问题可用try catch 处理,算术异常数字格式化为他的子类)和Error类(JVM出现问题了),同属于Throwable类子类可用Exception类中的e.printStackTrace();打印异常信息
7.1.4异常处理机制
一旦产生异常,则会产生一个异常实例化的对象,在try语句中进行对此异常的捕捉,产生的对象与catch语句中各个异常类型进行匹配,所有异常可以用Exception进行接受,必须范围大的异常靠后,直接使用Exception比较方便,若有多个异常最好分别捕获

package org.lxh.debugdemo;


public class DebugDemo {
    public static void main(String args[]) {
        System.out.println("计算开始");
        int i = 10;
        int j = 0;
        try {
            String str1 = args[0];
            String str2 = args[1];
            i = Integer.parseInt(str1);
            j = Integer.parseInt(str2);
            int temp = i / j;
            System.out.println("两个数字相处结果:" + temp);
            System.out.println("--------------------------");
        } catch(NumberFormatException e) {
            e.printStackTrace();
        }catch(ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }catch (ArithmeticException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            System.out.println("出现异常:" + e);
        }
        System.out.println("计算结束");
    }
}

7.2throws 与throws
throws声明的方法不处理异常,异常交给调用处处理,主方法使用throws抛给JVM,使用throw直接抛出异常的实例化对象

package org.lxh.debugdemo;

class Math {
    public int div(int i, int j) throws Exception {
        int temp = i / j;
        return temp;
    }
}

public class DebugDemo {
    public static void main(String args[]) {
        Math m = new Math();
        try {
            System.out.println(m.div(10, 0));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package org.lxh.debugdemo;

public class DebugDemo {
    public static void main(String args[]) {
        try {
            throw new Exception("自己抛出的异常");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
package org.lxh.debugdemo;

class Math {
    public int div(int i, int j) throws Exception {
        System.out.println("计算开始");
        int temp = 0;
        try {
            temp = i / j;
        } catch (Exception e) {
            throw e;
        } finally {
            System.out.println("计算结束");
        }
        return temp;
    }
}

public class DebugDemo {
    public static void main(String args[]) {
        Math m = new Math();
        try {
            System.out.println("除法操作:" + m.div(10, 0));
        } catch (Exception e) {
            System.out.println("异常产生" + e);
        }
    }
}

7.3Excveption 与RuntimeException
后者是前者的子类,后者使用throws不用用try catch,而且Integer为后者的子类,直接使用JVM进行处理
7.4自定义异常类

package org.lxh.debugdemo;

class MyException extends Exception {
    public MyException(String msg) {
        super(msg);
    }
}

public class DebugDemo {
    public static void main(String args[]) {
        try {
            throw new MyException("自定义异常");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

7.5断言
断言返回结果为boolean但不能将其作为条件判断语句,一般在开发中不提倡使用断言

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容