Java 为什么第一个编译而第二个不编译';t

Java 为什么第一个编译而第二个不编译';t,java,Java,您有两种选择: 捕获要抛出的异常: import static java.lang.System.*; import java.io.*; public class ExceptionDemo { public static void main(String args[]) { try { throw new Exception(); } finally { System.out.print("exce

您有两种选择:

捕获要抛出的异常:

import static java.lang.System.*;
import java.io.*;

public class ExceptionDemo {


    public static void main(String args[]) {
        try {
            throw new Exception();
        } finally {
            System.out.print("exception ");
        }
    }
}
要么让你的方法抛出它:

public static void main(String args[]) {
    try {
        throw new Exception();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.out.print("exception ");
    }
}

但无论如何,你必须处理好它。我自己的偏好是捕获并直接处理它。

第一个不会显式抛出任何异常,而第二个会,并且您不会以任何方式处理它(捕获它或进一步抛出)@jeroenvanevel不,它不会。有一个未打补丁的异常。@反斜杠:没关系,我没有注意到Ideone自动添加了它。请阅读所有关于已检查和未检查异常的内容。
public static void main(String args[]) {
    try {
        throw new Exception();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.out.print("exception ");
    }
}
public static void main(String args[]) throws Exception {
    try {
        throw new Exception();
    } finally {
        System.out.print("exception ");
    }
}