Reputation: 31
can I apply "Rethrowing Exceptions with Improved Type Checking" in Java 6 in prior while in the spec of Java, they said that this feature is just available for Java 7 and later? However, I try to write some source code in Java 6 and apply same way and get the same result...I don't know where is the incorrect in my code.
I attached some picture:
I don't know SourceCode2 and SourceCode3 is the same or not? But, when I execute both, I get the same result..
I'm very confuse that SourceCode2 is the other way to use more precise rethrow exception in Java or not?
And, why we use this feature?
Thank you for your help
SourceCode1 (both java 6 and 7)
Upvotes: 3
Views: 97
Reputation: 4387
Here is reference to Oracle's guides. Quote:
in Java SE 7, you can specify the exception types FirstException and SecondException in the throws clause in the rethrowException method declaration.
So answer is no. In Java 6, you can't change declaration of inherited method, even in terms of thrown exceptions.
In Java 6, if method is declared, that throws, let's say - Exception
, then all classes that inherits from that one must have methods that throws Exception
. No matter what. In Java 7 method of descendant class can be more precise in terms of re-throwing.
Upvotes: 2