0

这是我的代码:

class FinallyDemo {
    static void myMethod(int n) throws Exception{
        try {
            switch(n) {
                case 1: 
                    System.out.println("1st case");
                    return;
                case 3: 
                    System.out.println("3rd case");
                    throw new RuntimeException("3!");
                case 4: 
                    System.out.println("4th case");
                    throw new Exception("4!");
                case 2: 
                    System.out.println("2nd case");
            }
        catch (RuntimeException e) {
            System.out.print("RuntimeException: ");
            System.out.println(e.getMessage());
        } finally {
            System.out.println("try-block entered.");
        }
    }

    public static void main(String args[]){
        for (int i=1; i<=4; i++) {
            try {
                FinallyDemo.myMethod(i);
            } catch (Exception e){
                System.out.print("Exception caught: ");
                System.out.println(e.getMessage());
            }
            System.out.println();
        }
    }
}

现在,它不是这样工作的:

如果我在方法本身中有一个 try and catch 块,那么我不需要写

method_name(int n) throws Exception?

抛出异常的方法中的try-catch块不会阻止在抛出异常的方法中写入“抛出异常”吗?

4

7 回答 7

3

在您的示例中,案例 4 引发异常,而在 catch 中您只是在捕获 RuntimeException。由于没有异常捕获,因此您的方法需要声明它抛出异常。如果要为异常添加捕获,则不需要抛出异常。这将起作用。

static void myMethod(int n) {
    try {
        switch (n) {
            case 1:
                System.out.println("1st case");
                return;
            case 3:
                System.out.println("3rd case");
                throw new RuntimeException("3!");
            case 4:
                System.out.println("4th case");
                throw new Exception("4!");
            case 2:
                System.out.println("2nd case");
        }
    } catch (RuntimeException e) {
        System.out.print("RuntimeException: ");
        System.out.println(e.getMessage());
    } catch (Exception e) {
        System.out.print("Exception: ");
        System.out.println(e.getMessage());
    } 
    finally {
        System.out.println("try-block entered.");
    }
}
于 2011-10-17T04:52:44.270 回答
1

throws当且仅当捕获到抛出的异常类型(或者如果它扩展时,您才不需要该子句RuntimeException。在您的情况下,您抛出一个Exceptionwith the statement throw new Exception("4!");,但您只捕获 type RuntimeException

如果为 添加 catch 块Exception,则不再需要该throws子句。例如:

static void myMethod(int n) throws Exception{
    try {
        switch(n) {
        case 1: 
            System.out.println("1st case");
            return;
        case 3: 
            System.out.println("3rd case");
            throw new RuntimeException("3!");
        case 4: 
            System.out.println("4th case");
            throw new Exception("4!");
        case 2: 
            System.out.println("2nd case");
        }
    } catch (RuntimeException e) {
        System.out.print("RuntimeException: ");
        System.out.println(e.getMessage());
    } catch(Exception e) {
        System.out.print("Exception: ");
        System.out.println(e.getMessage());
    } finally {
        System.out.println("try-block entered.");
    }
}
于 2011-10-17T04:51:13.423 回答
1

是的,前提是您要捕获该方法可能引发的所有异常类型。

在您的代码中,您抛出一个Exception但不catch为其提供一个块(您只是在捕获RuntimeException),因此您必须将您的方法声明为 throwingException

你需要:

 ...
 catch (RuntimeException e) {
     System.out.print("RuntimeException: ");
     System.out.println(e.getMessage());
 } catch (Exception e) {
     System.out.print("Exception: ");
     System.out.println(e.getMessage());
 } finally {
 ...
于 2011-10-17T04:51:22.157 回答
0

抛出异常的方法中的try-catch块不会阻止在抛出异常的方法中写入“抛出异常”吗?

不,你总是可以声明抛出异常,即使你没有。

除此之外,这对于允许子类抛出它们很有用(因为它们不允许添加额外的抛出子句)。它还允许您稍后更改实现而不更改异常接口。

于 2011-10-17T04:53:21.977 回答
0

现在是两种类型的例外

  1. 异常的子类。
  2. RuntimeException 的子类

Exception 的子类被称为检查异常,编译器确保它们在 try/catch 块中或通过修饰符throws Exception(或子类)在方法上进行管理。

RuntimeException 的子类被称为未经检查的异常,编译不需要任何机制来管理它。

现在,如果您在方法上使用修饰符throws Exception(或子类),编译器将要求您使用 try/catch 对其进行管理。

于 2011-10-17T04:53:35.683 回答
0

由于您在 switch 中同时抛出 aRuntimeException和 an Exception,因此您要么需要同时捕获两者,要么方法需要抛出 theException以便可以在方法调用中进行处理myMethod

要同时使用:

catch (RuntimeException e) {
        System.out.print("RuntimeException: ");
        System.out.println(e.getMessage());
}catch (Exception e) {
        System.out.print("Exception: ");
        System.out.println(e.getMessage());
}

确保捕获的Exception始终是最后一个,否则它也会捕获它,RuntimeException因为它会扩展Exception

于 2011-10-17T04:54:24.817 回答
0

您正在以两种方式处理异常。首先,如果您像声明方法时所做的那样扩展直接调用 Exception 类

method_name(int n) throws Exception

这意味着无论方法中发生什么类型的异常,它总是能够捕获它,例如,如果在方法内发生算术异常或 NullPointerException 或 ArrayIndexOutOfBoundsException,您的上述声明将能够捕获每个和他们每一个人。因此,将 try catch 块放置在该方法中并没有实际目的,因为即使 RunTimeException 也是异常类层次结构的一部分。因此,如果我正确理解了您的问题,程序将执行然后从 catch 块中捕获异常,如果失败,它将从方法声明中捕获它。希望它能回答您的查询。

于 2011-10-17T04:58:07.473 回答