Java中常见的四种异常
1. ArithmeticException
算术异常,示例如下:
package project6;
public class project6 {
public static void main(String[] args) {
int a=10;
int b=0;
System.out.println(a/b);
}
}
运行结果图:
2. ArrayIndexOutOfBoundsException
数组越界异常,示例如下:
package project6;
public class project6 {
public static void main(String[] args) {
int[] a=new int[5];
System.out.println(a[5]=3);
}
}
运行结果图:
3. ClassCastException
类转换异常,代码如下:
package project6;
public class project6 {
public static void main(String[] args) {
Object a="abc";
Integer t=(Integer)a;
}
}
4. StackOverflowError
栈溢出错误,示例如下:
package project6;
public class project6 {
static int sum(int num){
return num+sum(num-1);
}
public static void main(String[] args) {
System.out.println(sum(10));
}
}
运行结果如图:
|