主方法的Java异常规范

主方法的Java异常规范,java,exception,Java,Exception,Java程序中的主方法不需要异常规范吗。例如,以下代码的工作原理完全相同,没有为main方法指定“throwsexcept” class Xcept extends Exception { public Xcept(){ } public Xcept(String msg){ super(msg); } } public class MyException { public void f() throws Xcept {

Java程序中的主方法不需要异常规范吗。例如,以下代码的工作原理完全相同,没有为main方法指定“throwsexcept”

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强制执行了这一点,但如果我将此规范排除在主方法之外,我不会得到编译时错误。

这是因为
except
永远不会从
main
方法中抛出,因为实际上
catch
方法中。。。
sed.f()
调用可能会导致抛出
except
,但它已被捕获并处理。

谢谢。我想我没有正确理解“抛出”。这很有帮助。
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");
        }
    }
}