java的错误分类

java的错误分类


java中的错误分为两大类:Error和Exception错误。

#
Error 是程序无法处理的错误,表示运行应用程序中较严重问题,修改程序本身是不能解决的。例如java运行时产生的系统内部错误(虚拟机错误),资源耗尽导致的错误。

Exception是异常类,它表示程序本身可以处理的错误。主要分为非运行异常和运行异常。
1.非运行时异常(编译异常)
IOException异常
表示由失败或中断的I / O操作产生的异常类。


SQLException异常
表示访问数据库错误或者数据库信息错误。

2.运行时异常(RuntimeException)
常见的运行时异常:
ClassCastException(类转换异常)
public class Demo{
public static void main(String[] args){
Object x = new Integer(0);
System.out.println((String)x);
}
}

StringIndexOutOfBoundsException(字符串角标越界异常)

public class Demo {
public static void main(String[] args){
String s = "abcdefg";
System.out.print(s.charAt(9));
}
}

ArrayIndexOutOfBoundsException(数组角标越界异常)

public class Demo {
public static void main(String[] args){
int [] arr = new int[4];
System.out.println("arr[0]="+arr[4]);
}
}

NullPointerException(空指针异常)

public class HelloWorld{
public static void main(String[] args){
int [] arr = new int[4];
arr[0] = 5;
System.out.println("arr[0]="+arr[0]);
arr = null;
System.out.println("arr[0]="+arr[0]);//报错
}
}

ArrayStoreException(数据存储异常,操作数组时类型不一致)

public class Test{
public static void main(String[] args){
Object x[] = new String[3];
x[0] = new Integer(0);
System.out.println(x[0]);
}
}

ArithmeticException(算术异常)

public class FindError {
public static void main(String[] args){
int x = 4,y = 0;
int result = x / y;
System.out.print(result);
}
}
原文地址:https://www.cnblogs.com/changpuyi/p/8596651.html