我只是想更好地理解這一點。是否所有的異常類型都在普通的舊「異常」之下?
我知道有很多不同exception types,根據我所做的一些閱讀,所有異常類型都被Exception捕獲。首先,我可以確信這是真的嗎?
try{
...
}
catch(Exception x){
//No matter what fails in the try block, x
//will always have a value since all exception
//types are caught under Exception? I guess
//What I really want to know, is will this ever
//Fail?
}
catch(SystemException x){
//This code will never execute since all
//exceptions are caught in the first catch?
}
接下來,該捕獲層次結構如何工作?如果Exception在頂部,那麼其他異常類型在Exception下面的一個級別中,或者是否有多個類型層級,例如Exception是ExceptionSomething的父級,這是ExceptionSomethingElse的父級?
編:
或者,如果我們有這樣的代碼:
try{
...
}
catch(SystemException x){
//If there is an exception that is not a SystemException
//code in this block will not run, right (since this is
//looking specifically for SystemExceptions)?
}
如果我不添加任何引發任何瘋狂的代碼,只捕獲異常應該足夠好嗎? – sooprise 2011-04-14 18:37:17
@sooprise是;在任何理智的代碼中,捕獲異常是很好的;但只做記錄等等。並且記得「拋出」,而不是「拋出」重擲。 – 2011-04-14 18:39:40