如果我有一个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?
}
我的想法是yes,因为一个RuntimeException
延伸Exception
。但是我有一些生产代码的行为并非如此。显然,如果答案是否定的,那么这就是我的答案;否则我需要深入挖掘并看看为什么我的代码会出错。提前致谢!
RuntimeException
源自Exception
,所以它会被抓住。
话虽如此,不要这样做!运行时异常应该被阻止,而不是被捕获。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)