当前位置:   article > 正文

java学习笔记47:运行时异常RuntimeException、已检查异常CheckedException_运行时异常(runtimeexception)

运行时异常(runtimeexception)

一、运行时异常RuntimeException

派生于RuntimeException的异常,如被 0 除、数组下标越界、空指针等,其产生比较频繁,处理麻烦,如果显式的声明或捕获将会对程序可读性和运行效率影响很大。 因此由系统自动检测并将它们交给缺省的异常处理程序(用户可不必对其处理)。
  这类异常通常是由编程错误导致的,所以在编写程序时,并不要求必须使用异常处理机制来处理这类异常,经常需要通过增加"逻辑处理来避免这些异常"。(逻辑问题)

1、示例:ArithmeticException异常:试图除以0

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

运行结果:
在这里插入图片描述
解决方案:解决如上异常需要修改代码:

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

2、空指针异常:
当程序访问一个空对象的成员变量或方法,或者访问一个空数组的成员时会发生空指针异常(NullPointerException)。
示例:NullPointerException异常(空指针异常)

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

运行结果:
在这里插入图片描述
解决空指针异常,通常是增加非空判断:

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

3、示例:ClassCastException异常

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

运行结果:
在这里插入图片描述
解决ClassCastException的典型方式:

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

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

示例:ArrayIndexOutOfBoundsException异常

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

运行结果:
在这里插入图片描述
解决数组索引越界异常的方式,增加关于边界的判断:

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]);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

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

运行结果:
在这里插入图片描述
数字格式化异常的解决,可以引入正则表达式判断是否为数字:

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

注意事项
  1. 在方法抛出异常之后,运行时系统将转为寻找合适的异常处理器(exception handler)。潜在的异常处理器是异常发生时依次存留在调用栈中的方法的集合。当异常处理器所能处理的异常类型与方法抛出的异常类型相符时,即为合适的异常处理器。
  2. 运行时系统从发生异常的方法开始,依次回查调用栈中的方法,直至找到含有合适异常处理器的方法并执行。当运行时系统遍历调用栈而未找到合适的异常处理器,则运行时系统终止。同时,意味着Java程序的终止。

二、CheckedException已检查异常

所有不是RuntimeException的异常,统称为Checked Exception,又被称为“已检查异常”,如IOException、SQLException等以及用户自定义的Exception异常。 这类异常在编译时就必须做出处理,否则无法通过编译。如下图所示:
在这里插入图片描述
异常的处理方式有两种:使用“try/catch”捕获异常、使用“throws”声明异常。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号