1 package com.yhqtv.demo01Exception; 2 /* 3 * 一、异常体系结构 4 *java.lang.Throwable 5 * ------java.lang.Error:一般不编写针对性的代码进行处理。 6 * ------java.lang.Exception:可以进行异常的处理 7 * ----------编译时异常(checked) 8 * -------IOException 9 * ----------FileNotFoundException 10 * -------ClassNotFoundException 11 * ----------运行时异常(unchecked) 12 * --------NullPointerException 13 * --------ArrayIndexOutOfBoundsException 14 * --------ClassCastException 15 * --------NumberFormatException 16 * --------InputMismatchException 17 * --------ArithmeticException 18 * 19 *面试题,常见的异常都有哪些?举例说明 20 * */ 21 22 import org.junit.Test; 23 24 import java.io.File; 25 import java.io.FileInputStream; 26 import java.util.Date; 27 import java.util.Scanner; 28 29 public class ExceptionTest { 30 //##################以下是编译时异常######################## 31 @Test 32 public void test7(){ 33 File file=new File("hello.txt"); 34 FileInputStream fis=new FileInputStream(file); 35 36 int data=fis.read(); 37 while (data!=-1) { 38 System.out.println((char) data); 39 data =fis.read(); 40 41 } 42 fis.close(); 43 44 } 45 //##################以下是运行时异常######################## 46 //ArithmeticException 47 @Test 48 public void test6(){ 49 int a=10; 50 int b=0; 51 System.out.println(a/b); 52 53 } 54 //InputMismatchException 55 @Test 56 public void test5(){ 57 // Scanner sc=new Scanner(System.in); 58 // int nextInt = sc.nextInt(); 59 } 60 //NumberFormatException 61 @Test 62 public void test4(){ 63 String str="123"; 64 str="abc"; 65 int i = Integer.parseInt(str); 66 } 67 68 //ClassCastException 69 @Test 70 public void test3(){ 71 Object obj=new Date(); 72 String str=(String)obj; 73 74 } 75 76 //ArrayIndexOutOfBoundsException 77 @Test 78 public void test2(){ 79 // int[] arr=new int[3]; 80 // System.out.println(arr[6]); 81 82 String str="abc"; 83 System.out.println(str.charAt(3)); 84 85 } 86 87 88 //NullPointerException 89 @Test 90 public void test1(){ 91 // int[] arr=null; 92 // System.out.println(arr[3]); 93 94 String str="abc"; 95 str=null; 96 System.out.println(str.charAt(3)); 97 } 98 99 }