标签:
一、异常的体系结构
import java.io.File; import java.io.FileInputStream; import java.util.Date; import java.util.Scanner; import org.junit.Test; public class TestException { //编译时异常 @Test public void test6(){ // FileInputStream fis = new FileInputStream(new File("hello.txt")); // int b; // while((b = fis.read()) != -1){ // System.out.println((char)b); // } // fis.close(); } //常见的运行时异常 //4.空指针异常:NullPointerExcetion @Test public void test5(){ // Person p = new Person(); // p = null; // System.out.println(p.toString()); String str = new String("AA"); str = null; System.out.println(str.length()); } //3.类型转换异常:ClassCastException @Test public void test4(){ Object obj = new Date(); String str = (String)obj; //String str1 = (String)new Date(); } //2.算术异常:ArithmeticException @Test public void test3(){ int i = 10; System.out.println(i / 0); } //1.数组下标越界的异常:ArrayIndexOutOfBoundsException @Test public void test2(){ int[] i = new int[10]; //System.out.println(i[10]); System.out.println(i[-10]); } @Test public void test1(){ Scanner s = new Scanner(System.in); int i = s.nextInt(); System.out.println(i); } } class Person{ }
标签:
原文地址:http://www.cnblogs.com/zhangfan94/p/4263320.html