我想知道的Java是如何發生以下情形拋出異常並捕獲異常?
public static void main(String[] args) throws IndexOutOfBoundsException, CoordinateException, MissionException, SQLException, ParserConfigurationException {
try {
doSomething();
} catch (Exception e) {
e.printStackTrace();
}
}
在上面的代碼中,我聲明的主要功能是引發許多不同的例外,但裏面的功能,我捕捉通用的異常。我想知道如何java內部?也就是說,doSomething()
會拋出一個IndexOutOfBounds異常,e.printStackTrace()會在最後的catch (Exception e) {...}
塊中被調用嗎?
我知道如果沒有在函數的throws區域中聲明的異常被引發,try/catch將處理它,但是在聲明中提到的異常呢?
+1只'Error'等'Throwable'都沒有抓到。 –
事情是,有時我會希望它在try/catch中處理,然後拋出。在這種情況下,我應該在異常結束時執行'throw e;'操作嗎? –
yeap,不要忘記,你可以創建自己的異常,並可能在catch塊中拋出它們。這就是所謂的鏈式異常http://docs.oracle.com/javase/tutorial/essential/exceptions/chained.html – MaVRoSCy