我只是想更好地理解这一点。是否所有的异常类型都在普通的旧“异常”之下?
我知道有很多不同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