Java异常中我们常见的异常主要是检查异常,其中检查异常zhuy主要有如下四种:
1、空指针异常,具体代码体现如下
String str = null;
try {
System.out.println(str.length());
}catch(Exception e){
System.out.println("空指针异常了");
}
2、数组下标越界异常
int []a = {1,2,3};
try {
for(int i = 0;i<=3;i++) {
System.out.println(a[i]);
}
}catch(Exception e) {
System.out.println("数组下标越界了!!!");
}
3、数据类型转换异常
class Animal(){
}
class Dog extends Animal(){
}
calss Cat extends Animal(){
}
Animal a1 = new Dog();
Animal a2 = new Cat();
Dog d1 = (Dog) a1;
Dog d2 = (Dog) a2;
3、算术异常,只要是分母为0的情况
int a= 12;
int b= 0;
try {
System.out.println(a/b);
}catch(Exception e) {
System.out.println("出现算术异常拉!!!");
}
来源:CSDN
作者:MJ17709005513
链接:https://blog.csdn.net/MJ17709005513/article/details/103791269