2
我們是否需要Java程序中主要方法的異常規範。例如,下面的代碼完全相同,沒有爲main方法指定「throws Xcept」。主要方法的Java異常規範
class Xcept extends Exception {
public Xcept(){
}
public Xcept(String msg){
super(msg);
}
}
public class MyException {
public void f() throws Xcept {
System.out.println("Exception from f()");
throw new Xcept("Simple Exception");
}
public static void main(String[] args) throws Xcept {
MyException sed = new MyException();
try {
sed.f();
} catch(Xcept e) {
e.printStackTrace();
}
finally {
System.out.println("Reached here");
}
}
}
,我讀了Java強制,但如果我排除這種規範的主要方法我沒有得到一個編譯時錯誤。
謝謝。我想我沒有正確理解「拋出」。這有助於。 –