将异常重新分配给catch参数 [英] Reassignment of an Exception to the catch parameter

查看:48
本文介绍了将异常重新分配给catch参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码.

Consider the following code.

import java.io.IOException;
import java.sql.SQLException;

public class ReassignmentICatch {

    public void couldThrowAnException() throws SQLException, IOException {}
    public void rethrow() throws SQLException, IOException {
      try {
         couldThrowAnException();
         System.out.println("Did not throw");
      } catch (Exception e) {   //Line-1
        e = new IOException();  //Line-2    
        throw e;                //Line-3        
      }
    }

    public static void main(String[] args) {
        ReassignmentICatch rc = new ReassignmentICatch();
        try {
            rc.rethrow();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

据我所知,java7中的 catch(Exception e)不能捕获像java6这样的所有异常,而是仅捕获方法 couldThrowAnException 的声明中提到的那些异常.另外,不允许为catch参数(e)重新分配新的异常,这就是为什么我们在 Line-3

遇到编译错误的原因Java6也不允许这种重新分配吗?我认为应该允许它在java6中重新分配,所以我在eclipse中更改了java-compiler的属性,并将合规级别设置为1.6,以查看此代码是否可以编译.

我无法预测的行为是:
1.Eclipse给出了相同的错误

As i know catch(Exception e) in java7 does not catch all exceptions like java6 instead it catches only those exceptions which are mentioned in the declaration of method couldThrowAnException. Also Reassigning a new exception to the catch parameter(e) is not allowed that's why we get compilation error at Line-3

Is this reassignment not allowed in java6 too. I thought it should be allowed to reassign in java6 so i changed the property of java-compiler in eclipse and set compliance level to 1.6 to see whether this code compiles or not.

The behaviour that i am not able to predict is:
1.Eclipse is giving same error

未处理的异常类型异常

Unhandled exception type Exception

对于Java 6 7和8.

2.当我尝试使用 javac -target 1.6 -source 1.6 ReassignmentICatch.java 进行命令行操作时

for java 6 7 and 8.

2.When i am trying with command line using javac -target 1.6 -source 1.6 ReassignmentICatch.java am getting

警告:[选项]引导程序类路径未与结合设置-source 1.6 ReassignmentICatch.java:18:错误:未报告的异常Exception;必须被嘲笑或声明被抛出

warning: [options] bootstrap class path not set in conjunction with -source 1.6 ReassignmentICatch.java:18: error: unreported exception Exception; must be caugh t or declared to be thrown

  throw e;
  ^ 1 error 1 warning 

并使用java7和java8可以编译并成功运行,并输出 Did not throw .为什么IDE和命令行没有给出相同的结果.

有人可以建议我这里缺少什么吗?
谢谢.

and with java7 and java8 i am able to compile and run it successfully with output Did not throw .Why IDE and command-line are not giving same result.

Can somebody please suggest me what I am missing here.
Thanks.

推荐答案

我不确定Java 6和Java 7在捕获异常方面的不同之处,因为我不知道那样的含义,您可以发布参考吗?

I'm not sure about what you mean by that Java 6 and Java 7 are different in how they catch exceptions as I'm not aware of anything like that, can you post a reference ?

异常是 throws子句异常.这些是方法本身无法捕获但希望它的调用者捕获的异常.

Exceptions mentioned in method declaration are throws clause exceptions. These are the exceptions that method itself doesn't catch but wants it's caller to catch.

分配catch变量时,您应该不会出错.但是,您得到的错误是因为您抛出了Exception类型,并且未在throws子句中声明它.

You should not be getting error when assigning to catch variable. The error that you're getting however is because you're throwing an Exception type and it's not declared in the throws clause.

请考虑以下代码

e = new IOException();  //Line-2    
throw e;                //Line-3

即使您已经分配了 e IOException 实例,但 e 的类型仍然是 Exception .未被捕获且未在throws子句中声明,因而被报告为错误.

Even though you've assigned e IOException instance, type of e is still Exception. Which is not caught and not declared in throws clause and thus reported as error.

有两种解决方案.您可以在throws子句中声明Exception,也可以在抛出它之前进行类型转换 e .

There are two solutions for it. You can either declare Exception in the throws clause or typecast e before throwing it.

public void rethrow() throws SQLException, IOException
//becomes
public void rethrow() throws Exception

throw e;
//becomes
throw (IOExceptoin)e;
//or
throw new IOException();

编辑*

您的链接是Java 7能够在编译时推断异常类型.即使将其分配给Super类类型,Java 7也能够推断出其原始类型,并且不会抱怨.这使我们不必将异常对象包装到 RuntimeException 对象中.

The difference between Java 6 and Java 7 as given in your link is that Java 7 is capable of deducing exception type at compile time. Even if it's assigned to a Super class type Java 7 will be able to deduce it's original type and not will not complain. This saves us from having to wrap the exception object into a RuntimeException object.

您的Eclipse和命令行输出不同的原因很可能是您尚未针对不同的Java版本修改Eclipse中的编译器设置.在Eclipse中更改项目的Java版本时,您需要更改以下内容

Most likely the reason of difference in your Eclipse and command line output is that you haven't modified the compiler settings in the Eclipse for different Java versions. When changing a Java version for a project in Eclipse you need to change following

  1. 构建路径中的Java系统库
  2. 编译器源合规性(首选项> Java> Compiler>编译器合规性级别)

在进行了两项更改之后,您应该能够看到正确的行为.

After making both the changes you should be able to see correct behavior.

这篇关于将异常重新分配给catch参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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