常见的运行时异常
NullPointerException(空指针异常)
int[] arr = null;
system.out.println(arr[1]);
ArrayIndexOutOfBoundsException(数组下标越界)
int[] arr = int[10];
system.out.println(arr[10]);
StringIndexOutOfBoundsException(字符串下标越界)
String str = "abc";
system.out.println(str[3]);
ClassCastException(类型转换异常)
Object obj = new Date();
String str = (String)obj;
NumberFormatException(数字格式异常)
String str = "abc";
int i = Integer.parseInt(str);
InputMismatchException(输入不匹配异常)
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
//控制台输入非数字报错
ArithmeticException(算数异常)
int x = 5/0;
常见的编译时异常
public class Test {
public static void main(String[] args) {
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);//FileNotFoundException(未找到文件异常)
int data = fis.read();//IOException(输入输出异常)
while(data != -1){
System.out.print((char)data);
data = fis.read();
}
fis.close();
}
}
}