生活随笔
收集整理的這篇文章主要介紹了
Java中异常机制
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、概述
異常是一套用于反饋和處理問(wèn)題的機(jī)制Throwable 是所有異常和錯(cuò)誤的頂級(jí)父類Throwable派生出兩個(gè)類Error和Exeption 二、Error
出現(xiàn)在代碼邏輯沒(méi)有錯(cuò)誤,因外界因素或環(huán)境因素導(dǎo)致的嚴(yán)重錯(cuò)誤的情況。一旦出現(xiàn),無(wú)法修改
三、Exception
-異常,代表了程序在編譯和運(yùn)行時(shí)發(fā)生的各種不期望發(fā)生的事件。是java異常處理的核心。非檢查是異常(運(yùn)行時(shí)異常): -編譯時(shí)不會(huì)報(bào)錯(cuò),運(yùn)行時(shí)報(bào)錯(cuò),往往是語(yǔ)法正確,但是邏輯錯(cuò)誤。?編譯前可以處理,也可以不處理。一個(gè)小的父類 --- RuntimeException 檢查異常(編譯時(shí)異常): 編譯時(shí)已經(jīng)出現(xiàn),一旦出現(xiàn),則必須處理要么拋出,要么捕獲 四、異常的捕獲
public static void main(String[] args) {// System.out.println(1 / 0);try {int[] arr = new int[3];System.out.println(arr[5]);} catch (ArrayIndexOutOfBoundsException e) {System.out.println();}}
多個(gè)異??梢酝瑫r(shí)處理,用多個(gè)catch捕獲如果所有異常的處理方式都一樣,可以用統(tǒng)一的Exception一次捕獲多個(gè)異常分組處理,在同一組異常之間可以用"|"隔開。(從JDK1.7開始)try {String msg = readFile(null);System.out.println(msg);} catch (PathNotExistException | FileCannotReadException e) {// 打印棧軌跡e.printStackTrace();// System.out.println(e.getMessage());} catch (Exception e) {} finally {System.out.println("文件釋放");}
如果方法拋出的是父類異常,則必須要一以父類異常捕獲在捕獲異常 時(shí),要求先捕獲子類異常,在捕獲父類異常。否則子類異常將永遠(yuǎn)無(wú)法捕獲到。 五、排查異常
查看異常的名字查看異常信息從棧軌跡尾端開始看起(控制臺(tái)從上至下)。 注意:如果異常最終拋給了JVM,那么JVM默認(rèn)打印 這個(gè)異常的棧軌跡。
六、異常的自定義
當(dāng)一個(gè)類繼承Exception,則默認(rèn)是編譯時(shí)異常當(dāng)一個(gè)類繼承RuntimeException時(shí),才是運(yùn)行是異常。一個(gè)類繼承運(yùn)行時(shí)異常類,那么這個(gè)類就是運(yùn)行時(shí)異常;反之,則為編譯時(shí)異常。 public class Demo_01 {public static void main(String[] args) {try {String str = readFile("C:\\ a.txt");} catch (FileCannotReadException e) {//打印異常信息System.out.println(e.getMessage());//打印棧軌跡e.printStackTrace();//查看異常: 1. 查看異常名字 2. 查看異常信息 3. 從棧軌跡尾端開始排查(控制臺(tái)中從上致下)//如果異常最終拋給了JVM,JVM默認(rèn)打印這個(gè)異常的棧軌跡}}private static String readFile(String string) throws FileCannotReadException {//如果是是以C開頭,則認(rèn)為這個(gè)文件不可被讀取if(string == null){//運(yùn)行時(shí)異常,可以處理,也可以不處理throw new NullPointerException("路徑不能為空");}elseif(string.startsWith("C")){//編譯時(shí)異常一定要處理throw new FileCannotReadException("文件不能被讀取");}return null;}
}
//當(dāng)一個(gè)類繼承Exception時(shí),默認(rèn)是編譯時(shí)異常//如果一個(gè)類繼承的時(shí)RuntimeException,才會(huì)稱為一個(gè)運(yùn)行時(shí)異常//一個(gè)類繼承編譯時(shí)異常,則這個(gè)類時(shí)編譯時(shí)異常。反之為運(yùn)行時(shí)異常。
class FileCannotReadException extends Exception{public FileCannotReadException() {}public FileCannotReadException(String msg){super(msg);}
}
七、異常方法的重寫和重載
異常不會(huì)影響方法的重載。當(dāng)一個(gè)類中方法名一致,但參數(shù)列表不一致時(shí)發(fā)生方法的重載,方法的重載和修飾符、返回值類型、異常都沒(méi)有關(guān)系。在重寫父類的異常方法時(shí),子類重寫的方法拋出的異常不能超出 父類異常方法拋出異常的范圍(編譯時(shí)異常的要求)。方法重寫時(shí)必須遵循兩小兩大一小原則。(參考:http://blog.csdn.net/chou_out_man/article/details/77976679)子類不能拋出范圍比父類 更大的異常 --- 針對(duì)的是編譯時(shí)異常 , 但對(duì)運(yùn)行時(shí)異常 。?class A {public void m() throws IOException {}public void m(int i) throws SQLException {}}class B extends A {// 子類不能拋出比父類更多的異常。// 在方法重寫的時(shí)候,子類中重寫的方法拋出的異常不能超過(guò)父類。---要求的是編譯時(shí)異常public void m() throws EOFException, FileNotFoundException, NullPointerException {}}
八、finally關(guān)鍵字的運(yùn)用
無(wú)論如何都會(huì)執(zhí)行一次。往往用于一些資源需要回收等一系列 善后處理。public static void main(String[] args) {try {Scanner s = new Scanner(System.in);System.out.println(s.nextInt() / s.nextInt());s.close();} catch (Exception e) {e.printStackTrace();} finally {// 無(wú)論代碼中出現(xiàn)異常與否,都會(huì)執(zhí)行一次。System.out.println("執(zhí)行完畢");}}
無(wú)論try的代碼中是否有異常,finally中的代碼都會(huì)執(zhí)行一次。finally執(zhí)行在try中return和異常發(fā)生之前。補(bǔ)充案例:public static void main(String[] args) {System.out.println(m());}@SuppressWarnings("finally")private static int m() {try {return 1;} catch (Exception e) {return 2;} finally {try {return 4;} finally {return 5;}}} // 結(jié)果是5
public static void main(String[] args) {System.out.println(m());}private static int m() {int i = 5;// 對(duì)于程序而言,是從上到下從左到右依次編譯運(yùn)行的。try {// return 5;-> i->6return i++;} finally {// i = 6; -> 7i++;System.out.println("Finally:" + i);}}public static void main(String[] args) {System.out.println(m());}public static Person m() {Person p = new Person();try {p.setName("翠花");p.setAge(16);// return p; -> 實(shí)際上返回的是p的地址return p;} finally {p = new Person();p.setName("如花");p.setAge(18);}}
總結(jié)
以上是生活随笔為你收集整理的Java中异常机制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。