• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java 7 rethrow in exceptions

 
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rethrow.java:25: error: unreported exception FirstException; must be caught or declared to be thrown
obj.test();
^
1 error

Even in java 7 I'm getting that same error



Use more precise rethrow in exceptions
The Java SE 7 compiler performs more precise analysis of rethrown exceptions than earlier releases of Java SE. This enables you to specify more specific exception types in the throws clause of a method declaration.

Consider the following example:

public class FirstException extends Exception {
// ...
}


public class SecondException extends Exception {
// ...
}


public void rethrowException(String exceptionName) throws Exception {
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}


This examples's try block could throw either FirstException or SecondException. Suppose you want to specify these exception types in the throws clause of the rethrowException method declaration. In releases prior to Java SE 7, you cannot do so. Because the exception parameter of the catch clause, e, is type Exception, and the catch block rethrows the exception parameter e, you can only specify the exception type Exception in the throws clause of the rethrowException method declaration.

However, in Java SE 7, you CAN specify the exception types FirstException and SecondException in the throws clause in the rethrowException method declaration. The Java SE 7 compiler can determine that the exception thrown by the statement throw e; must have come from the try block, and the only exceptions thrown by the try block can be FirstException and SecondException. Even though the exception parameter of the catch clause, e, is type Exception, the compiler can determine that it is an instance of either FirstException or SecondException:

public void rethrowException(String exceptionName) throws FirstException, SecondException {
try {
// ...
} catch (Exception e) {
throw e;
}
}


This analysis is disabled if the catch parameter is assigned to another value in the catch block. However, if the catch parameter is assigned to another value, you must specify the exception type Exception in the throws clause of the method declaration.

In detail, in Java SE 7, when you declare one or more exception types in a catch clause, and rethrow the exception handled by this catch block, the compiler verifies that the type of the rethrown exception meets the following conditions:

The try block is able to throw it.

There are no other preceding catch blocks that can handle it.

It is a subtype or supertype of one of the catch clause's exception parameters.

The Java SE 7 compiler allows you to specify the exception types FirstException and SecondException in the throws clause in the rethrowException method declaration because you can rethrow an exception that is a supertype of any of the types declared in the throws.

In releases prior to Java SE 7, you cannot throw an exception that is a supertype of one of the catch clause's exception parameters. A compiler from a release prior to Java SE 7 generates the error, "unreported exception Exception; must be caught or declared to be thrown" at the statement throw e;. The compiler checks if the type of the exception thrown is assignable to any of the types declared in the throws clause of the rethrowException method declaration. However, the type of the catch parameter e is Exception, which is a supertype, not a subtype, of FirstException and SecondException.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meeta gaur wrote:
Rethrow.java:25: error: unreported exception FirstException; must be caught or declared to be thrown
obj.test();
^
1 error

Even in java 7 I'm getting that same error


I'm not quite sure what your problem is, but you are not showing us all the relevant code. In your main method you are creating an instance of a Test class, but the code you've posted is for the Rethrow class. So you have a Test class somewhere with a test method that throws a FirstException checked exception.
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry , I updated codes.As written i should get that error prior to java 7 version but even in java 7 i 'm getting that error so what's new ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meeta gaur wrote:Sorry , I updated codes.


Well you need to update the eror message as well as that refers to FirstException which is not used in your code.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The cod you've got there should have an error in any version of Java. When you catch the exception you declare it as an Exception. Therefore e has an Exception reference type. You then rethrow that, but the method isn't defined to throw Exception. Java 7 gives you an alternative way of getting round this (the multi-catch), but it does not make that code valid.

However, as Joanne says, your code has no reference at all to a FirstException class, so the error you've mentioned is not caused by the code you've posted.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:The cod you've got there should have an error in any version of Java. When you catch the exception you declare it as an Exception. Therefore e has an Exception reference type. You then rethrow that, but the method isn't defined to throw Exception. Java 7 gives you an alternative way of getting round this (the multi-catch), but it does not make that code valid.


Actually that code is valid in Java 7 (that's what the documentation the OP posted is all about). However the error message is nothing to do with that - it's saying that test is declared to throw MyException1 and MyException2, but the call to it on line 25 doesn't handle these exceptions.
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

MyException1
Exception in thread "main" MyException1
at Rethrow.test(Rethrow.java:12)
at Rethrow.main(Rethrow.java:24)

Now is it correct ?
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting only this that in java 7 even i re-throw supertype reference but i can declare subtype exceptions.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:
Actually that code is valid in Java 7 (that's what the documentation the OP posted is all about). However the error message is nothing to do with that - it's saying that test is declared to throw MyException1 and MyException2, but the call to it on line 25 doesn't handle these exceptions.



Sorry, you're quite right. I'd got it into my head that it wasn't OK because you can assign to e within the catch block. But the documentation covers that specifically. Should have read more carefully - thanks!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meeta gaur wrote:I'm getting only this that in java 7 even i re-throw supertype reference but i can declare subtype exceptions.


Which appears to be exactly what the documentation (I presume that's what it is) is telling you: Since you don't attempt to reassign or wrap 'e', but simply re-throw it, the v7 compiler is smart enough to figure that it can only be of type MyException1 or MyException2.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic