在catch子句中抛出异常 [英] Throw exception inside catch clause
问题描述
class PreciseRethrow {
public static void main(String [] str ){
try {
foo();
} catch(NumberFormatException ife){
System.out.println(ife);
}
}
static private void foo()throws NumberFormatException {
try {
int i = Integer.parseInt(ten);
} catch(Exception e){
throw e;
}
}
}
和:
class PreciseRethrow {
public static void main(String [] str){
try {
foo );
} catch(NumberFormatException ife){
System.out.println(ife);
}
}
static private void foo()throws NumberFormatException {
try {
int i = Integer.parseInt(ten);
} catch(Exception e){
throw new Exception();
}
}
}
在第二种情况下,我得到编译当我在catch子句中抛出新的异常()时,错误未处理的异常类型异常。你可以解释一下为什么在第一种情况下一切都可以,但在第二个我得到编译错误?在这两种情况下,我抛出异常,但在第二种情况下,我创建新的异常实例(这仅在这两个例子之间beetwen)。感谢您的帮助。
首先:您的第一个示例不使用Java 6编译 。
请参阅此链接获取更多信息。基本上,从Java 7开始,编译器可以更准确地分析从 try
块抛出的异常;即使您捕获一个或多个抛出的异常的超类,并以原样(即没有转换)重新抛出,编译器也不会抱怨。然而,Java 6及更早版本将 抱怨。
然而,在第二个例子中,您推翻了一个新的实例异常
。实际上,这与签名不符。
总而言之,这意味着使用Java 7,您可以这样做,但不能使用Java 6:
public void someMethod()
throws MyException
{
try {
throw new MyException ();
} catch(Exception e){//注意:异常,而不是MyException
throw e;
}
}
Java 6只看到catch参数是类型异常
;对于Java 6,这与方法签名 - >编译错误不匹配。
Java 7看到try块只能抛出 MyException
。因此,方法签名匹配 - >无编译错误。
但不要这样做;)这很令人困惑...
I have two snippet of code :
class PreciseRethrow {
public static void main(String[] str) {
try {
foo();
} catch (NumberFormatException ife) {
System.out.println(ife);
}
}
static private void foo() throws NumberFormatException {
try {
int i = Integer.parseInt("ten");
} catch (Exception e) {
throw e;
}
}
}
and :
class PreciseRethrow {
public static void main(String[] str) {
try {
foo();
} catch (NumberFormatException ife) {
System.out.println(ife);
}
}
static private void foo() throws NumberFormatException {
try {
int i = Integer.parseInt("ten");
} catch (Exception e) {
throw new Exception();
}
}
}
In second case I got compile error "Unhandled exception type Exception" when I throw new Exception () in catch clause. Can You explain me Why in first case everything is ok but in second i get compile error ? In both case I throw Exception but in second case i create new instance of exception (this in only difference beetwen this two examples). Thanks for help.
First of all: your first example does not compile with Java 6 either. It will however compile with Java 7 due to new features in exception handling.
See this link for more information. Basically, starting with Java 7, the compiler can analyze exceptions thrown form a try
block more precisely; and even if you catch a superclass of one or more thrown exceptions and rethrow it "as is" (ie, without a cast), the compiler will not complain. Java 6 and earlier, however, will complain.
In your second example however you rethrow a new instance of Exception
. And indeed, this does not match the signature.
All in all, it means that with Java 7, you can do that, but not with Java 6:
public void someMethod()
throws MyException
{
try {
throw new MyException();
} catch (Exception e) { // Note: Exception, not MyException
throw e;
}
}
Java 6 only sees that the catch parameter is of type Exception
; and for Java 6, this does not match the method signature --> compile error.
Java 7 sees that the try block can only throw MyException
. The method signature therefore matches --> no compile error.
But don't do this ;) It is rather confusing...
这篇关于在catch子句中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!