如果我有一个try抛出RuntimException子类的块,后续catch块是否可以将其捕获为Exception?特别:
public class MyAppException extends RuntimeException {
// ....
}
// In some other part of the code:
try {
// Executing this results with doSomething() throwing a MyAppException.
int x = doSomething();
} catch(Exception exc) {
// Does the thrown MyAppException get caught here?
}
Run Code Online (Sandbox Code Playgroud)
我的想法是肯定的,因为RuntimeException延伸Exception.但是我有一些不符合这种方式的生产代码.很明显,如果答案是否定的,那就是我的答案; 否则我需要深入了解为什么我的代码坏了.提前致谢!
如果 catch(Exception) 没有捕捉到您的 RuntimeException,那么您的应用程序就没有按照您的想法运行。
try {
throw new RuntimeException();
} catch (Exception e) {
System.out.println("Caught "+e);
}
Run Code Online (Sandbox Code Playgroud)
印刷
Caught java.lang.RuntimeException
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14462 次 |
| 最近记录: |