7

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.

2
  • @prash: Did you forget to actually vote? Commented Apr 13, 2014 at 14:58
  • Yes! Infact I did from mobile. And request not completed. 3g was veryslow. Did it now. Thanks. @Keppil Commented Apr 13, 2014 at 21:14

7 Answers 7

7

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...

Sign up to request clarification or add additional context in comments.

1 Comment

Well explained especially when concluded
2

The difference is that the compiler can see that the only checked exception that can be rethrown in the first example is a NumberFormatException. (You are not creating any new exceptions.) Therefore it is happy with the throws NumberFormatException { declaration in the header.

In the second example however, you are explicitly throwing a checked exception that is not declared in the header, so predictably you get a compiler error.

1 Comment

I understood this only after reading @fge. Difference between java6 and java7. But good
0

In the first code snippet, you are re-throwing the NumberFormatException that could possibly come up. In the second code snippet, you are throwing just general Exception, which is not NumberFormatException, as the method has declared.

Comments

0

You are attempting to throw an Exception, however you declare that you will throw a NumberFormatException, which is a subtype of Exception. All NumberFormatExceptions are Exceptions, however not all Exceptions are NumberFormatException, so it is not allowed. In the first example however, although you are catching an Exception, the compiler and IDE know that it will only ever be a NumberFormatException, which is safe.

Comments

0

The compiler can determine in the case of throw e, that it can only be a checked exception of type NumberFormatException which is already declared in the throws clause.

In the latter case you're trying to throw the checked Exception which needs to be declared in the throws clause or caught.

Comments

0

Here ,You are throwing new Exception() from catch block but you have mentioned your method foo() can throw only NumberFormatException() which is lower in hierarchy

 static private void foo() throws NumberFormatException { try { int i = Integer.parseInt("ten"); } catch (Exception e) { throw new Exception(); } } 

Comments

0

In Both the case compile time error comes.Because you are try to throw a checked exception named Exception. You have to do one of two things:

use try construct in catch block OR add Exception in throws clause of foo()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.