免责声明:我没有访问java编译器也不能安装IDE,我的工作空间不给我足够的权限。是否明确抛出异常向上抛出?
我试图理解Java是如何处理异常交易和偶然发现了这个问题:
如果子类明确地抛出捕获所有异常的catch块中的例外,它是泛起?例如,考虑下面的代码行:
public Class someClass {
public int value;
public someClass() {
value = 1;
try {
value ++;
if(value == 2) {
throw new Exception("value is 2");
}
} catch (exception e) {
System.out.println("I caught an exception.");
throw new Exception("Does this exception get thrown upwards?");
System.out.println("will this line of code get printed after the previously thrown exception?");
} finally {
return;
}
}
}
public class anotherClass {
public static void main throws Exception{
someClass someclass = new someClass(); // will this class catch the second explicitly thrown exception?
}
}
因此,一个新的异常是在try块抛出,通过下面的catch块捕获。第二个投掷陈述去哪里?如果有的话,它会进入调用类吗?另外,println语句会在引发异常后执行,即使它不在finally块中?
谢谢。
您已拥有该代码。你为什么不跑它看看会发生什么? –
是的,第二个冒泡并且抛出后的'println'语句不会被执行。一个好的IDE将显示第二个'println'作为不可达代码。尝试Eclipse和FindBugs插件。 –
@ThomasWeller我目前的工作电脑没有Java,我没有安装Java的权限,我也不能联系管理员。 – noobcoder