Java异常处理-异常体系结构
import java.io.File; import java.io.FileInputStream; import java.util.Date; import java.util.InputMismatchException; import java.util.Scanner; /** * 一、异常体系结构 * 1.java.lang.Throwable * ---java.lang.Error:一般不编写针对性的代码进行处理 * ---java.lang.Exception:可以进行异常处理 * ---编译时异常(checked) * ---IOException * ---FileNotFoundException * ---ClassNotFoundException * ---运行时异常(unchecked,RuntimeException) * ---NullPointerException * ---ArrayIndexOutOfBoundsException * ---ClassCastException * ---NumberFormatException * ---InputMismatchException * ---ArithmeticException * * @author orz */ public class ExceptionTest { public static void main(String[] args) { //编译时异常 /*File file=new File("hello.txt"); FileInputStream fis=new FileInputStream(file); int data=fis.read(); while (data!=-1) { System.out.println((char)data); data=fis.read(); } fis.close(); */ //*****************************************// //运行时异常 //空指针异常NullPointerException /*int [] arr=null; System.out.println(arr[3]);*/ /*String str="abc"; str=null; System.out.println(str.charAt(0));*/ //数组角标越界ArrayIndexOutOfBoundsException /*int [] arr=new int[10]; System.out.println(arr[11]);*/ //StringIndexOutOfBoundsException /*String str="abc"; System.out.println(str.charAt(4));*/ //ClassCastException /* Object obj=new Date(); String str=(String)obj;*/ //NumberFormatException /*String str="123"; str="abc"; int num=Integer.parseInt(str); System.out.println(num);*/ //InputMismatchException /*Scanner scanner=new Scanner(System.in); int score=scanner.nextInt(); System.out.println(score);*/ //ArithmeticException /*int a=10; int b=0; System.out.println(a/b); scanner.close();*/ // } }