zoukankan      html  css  js  c++  java
  • RuntimeException运行时异常

      派生于RuntimeException的异常,如被 0 除、数组下标越界、空指针等,其产生比较频繁,处理麻烦,如果显式的声明或捕获将会对程序可读性和运行效率影响很大。 因此由系统自动检测并将它们交给缺省的异常处理程序(用户可不必对其处理)。

          这类异常通常是由编程错误导致的,所以在编写程序时,并不要求必须使用异常处理机制来处理这类异常,经常需要通过增加“逻辑处理来避免这些异常”。

    【示例】ArithmeticException异常:试图除以0

    1
    2
    3
    4
    5
    6
    public class Test3 {
        public static void main(String[] args) {
            int b=0;
            System.out.println(1/b);
        }
    }

        

    图6-4 ArithmeticException异常.png

          解决如上异常需要修改代码:

    1
    2
    3
    4
    5
    6
    7
    8
    public class Test3 {
        public static void main(String[] args) {
            int b=0;
            if(b!=0){
                System.out.println(1/b);
            }
        }
    }

          当程序访问一个空对象的成员变量或方法,或者访问一个空数组的成员时会发生空指针异常(NullPointerException)。怎么处理?

    【示例】NullPointerException异常

    1
    2
    3
    4
    5
    6
    public class Test4 {
    public static void main(String[] args) {
    String str=null;
    System.out.println(str.charAt(0));
    }
    }

    图6-5 NullPointerException异常.png

          解决空指针异常,通常是增加非空判断:

    1
    2
    3
    4
    5
    6
    7
    8
    public class Test4 {
        public static void main(String[] args) {
            String str=null;
            if(str!=null){
                System.out.println(str.charAt(0));
            }
        }
    }

          在引用数据类型转换时,有可能发生类型转换异常(ClassCastException)。

    【示例】ClassCastException异常

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    class Animal{
         
    }
    class Dog extends Animal{
         
    }
    class Cat extends Animal{
         
    }
    public class Test5 {
        public static void main(String[] args) {
            Animal a=new Dog();
            Cat c=(Cat)a;
        }
    }

    图6-6 ClassCastException异常.png

          解决ClassCastException的典型方式:

    1
    2
    3
    4
    5
    6
    7
    8
    public class Test5 {
        public static void main(String[] args) {
            Animal a = new Dog();
            if (a instanceof Cat) {
                Cat c = (Cat) a;
            }
        }
    }

          当程序访问一个数组的某个元素时,如果这个元素的索引超出了0~数组长度-1这个范围,则会出现数组下标越界异常(ArrayIndexOutOfBoundsException)。

    【示例】ArrayIndexOutOfBoundsException异常

    1
    2
    3
    4
    5
    6
    public class Test6 {
        public static void main(String[] args) {
            int[] arr = new int[5];
            System.out.println(arr[5]);
        }
    }

    图6-7 ArrayIndexOutOfBoundsException异常.png

          解决数组索引越界异常的方式,增加关于边界的判断:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class Test6 {
        public static void main(String[] args) {
            int[] arr = new int[5];
            int a = 5;
            if (a < arr.length) {
                System.out.println(arr[a]);
            }
        }
    }

          在使用包装类将字符串转换成基本数据类型时,如果字符串的格式不正确,则会出现数字格式异常(NumberFormatException)。

    【示例】NumberFormatException异常

    1
    2
    3
    4
    5
    6
    public class Test7 {
        public static void main(String[] args) {
            String str = "1234abcf";
            System.out.println(Integer.parseInt(str));
        }
    }

    图6-8 NumberFormatException异常.png

          数字格式化异常的解决,可以引入正则表达式判断是否为数字:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    public class Test7 {
        public static void main(String[] args) {
            String str = "1234abcf";
            Pattern p = Pattern.compile("^\d+$");
            Matcher m = p.matcher(str);
            if (m.matches()) { // 如果str匹配代表数字的正则表达式,才会转换
                System.out.println(Integer.parseInt(str));
            }
        }
    }

    注意事项

          1. 在方法抛出异常之后,运行时系统将转为寻找合适的异常处理器(exception handler)。潜在的异常处理器是异常发生时依次存留在调用栈中的方法的集合。当异常处理器所能处理的异常类型与方法抛出的异常类型相符时,即为合适的异常处理器。

          2. 运行时系统从发生异常的方法开始,依次回查调用栈中的方法,直至找到含有合适异常处理器的方法并执行。当运行时系统遍历调用栈而未找到合适的异常处理器,则运行时系统终止。同时,意味着Java程序的终止。

  • 相关阅读:
    C语言博客作业03--函数
    C博客作业02--循环结构
    C博客作业01--分支、顺序结构
    我的第一篇博客
    迭代购物车Dao&&GUI
    Java购物车大作业01
    DS-查找
    DS-图
    DS--树
    DS博客作业02--栈和队列
  • 原文地址:https://www.cnblogs.com/huaxiansheng/p/15316078.html
Copyright © 2011-2022 走看看