从JDK 1.7开始,从catch块抛出异常对象不需要throws子句!为什么会这样呢? [英] JDK 1.7 onwards, throwing an exception object from catch block does not require a throws clause!!! Why is this so?

查看:278
本文介绍了从JDK 1.7开始,从catch块抛出异常对象不需要throws子句!为什么会这样呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天在java编程时遇到了一个奇怪的情景。我在我的方法中有一个try..catch块,它没有任何throws子句,我能够抛出catch块中捕获的异常对象。 它是Exception类的一个对象,因此它不是一个未被检查的异常。另外,如果发生异常,它不会打印stacktrace,而是仅仅是被异常吞噬。



以下是我的代码示例,

  public class ExceptionTest {

public void test(){
try
{
//可能抛出异常的一些代码。
}
catch(Exception ex)
{
//编译器应该要求我在签名中有一个* throws Exception *,当我抛出一个异常对象时。
throw ex;
}
}

}

如果我抛出一个新的异常对象而不是捕获的异常对象,编译器要求我在方法签名中有一个throws子句。


注意:在Java 7或8中运行时,我正面临这种情况。


我想知道,抛出的对象将在哪里?任何有任何想法的人都可以...

解决方案

如果尝试块不能抛出任何检查异常。在这一点上,编译器知道由 catch 块捕获的唯一异常必须是未经检查的异常,因此可以重新引导。请注意,如果您在 catch 块中的 ex 分配了不同的值,则编译器将无法再那个保证。目前, ex 有效地终止。



如果你试图打电话这被声明在 try 块中抛出一个检查的异常,代码将无法按预期编译。



例如:

  public class ExceptionTest {

public void test(){
try {
foo();
} catch(Exception ex){
throw ex;
}
}

public void foo()throws java.io.IOException {
}
}

发生错误:

  ExceptionTest。 java:12:error:未报告的异常IOException;必须被抓住或声明被抛出
throw ex;
^

关于异常去的位置 - 如果try块中的代码抛出未经检查的异常,它会正常传播。尝试这样:

  public class ExceptionTest {

public static void main(String [] args){
test();
}

public static void test(){
try {
String x = null;
x.length();
} catch(Exception ex){
throw ex;
}
}
}

运行给出以下输出,如预期的那样:

 线程main中的异常java.lang.NullPointerException在ExceptionTest.test(ExceptionTest)中为
。 java:10)
在ExceptionTest.main(ExceptionTest.java:4)

JLS 11.2.2 记录一个语句可以有什么异常throw - 如果没有可以抛出的检查异常,你的代码将只会被编译。


I came across a weird scenario in java today while coding around. I have a try..catch block in my method which does not have any throws clause and I am able to throw the exception object caught in the catch block. It is an object of the Exception class, hence it is not an unchecked exception. Also, It is not printing the stacktrace if exception arises instead the exception is just getting swallowed.

Below is my code example,

public class ExceptionTest {

    public void test() {
        try
        {
            // Some code which may throw exception.
        }
        catch(Exception ex)
        {
            // Compiler should ask me to have a *throws Exception* in the signature, when I am throwing an exception object.
            throw ex;
        }
    }

}

However, if I am throwing a new exception object instead of the caught exception object, compiler is asking me to have a throws clause in the method signature.

N.B: I am facing this situation when running in Java 7 or 8.

I am wondering, where is the thrown object going to? Anyone with any idea on this please...

解决方案

You'll see this if the code in the try block can't throw any checked exception. At that point, the compiler knows that the only kind of exception caught by the catch block has to be an unchecked exception, and so it can therefore be rethrown. Note that if you assigned a different value to ex within the catch block, the compiler would no longer be able to have that assurance. At the moment, ex is effectively final.

If you try to call something that is declared to throw a checked exception within the try block, the code will fail to compile as expected.

For example:

public class ExceptionTest {

    public void test() {
        try {
            foo();
        } catch(Exception ex) {
            throw ex;
        }
    }

    public void foo() throws java.io.IOException {
    }
}

Gives an error of:

ExceptionTest.java:12: error: unreported exception IOException; must be caught or declared to be thrown
        throw ex;
        ^

As for where the exception "goes" - if the code in the try block throws an unchecked exception, it gets propagated as normal. Try this:

public class ExceptionTest {

    public static void main(String[] args) {
        test();
    }

    public static void test() {
        try {
            String x = null;
            x.length();
        } catch(Exception ex) {
            throw ex;
        }
    }
}

Running that gives the following output, as expected:

Exception in thread "main" java.lang.NullPointerException
        at ExceptionTest.test(ExceptionTest.java:10)
        at ExceptionTest.main(ExceptionTest.java:4)

JLS 11.2.2 documents what exceptions a statement can throw - your code will only compile if there are no checked exceptions that can be thrown.

这篇关于从JDK 1.7开始,从catch块抛出异常对象不需要throws子句!为什么会这样呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆