1. 程式人生 > 實用技巧 >Java異常處理-異常體系結構

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();*/ // } }