File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unchecked exceptions caught!!!

 
Chandra Bairi
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleas consider the following code
class A {
void method() throws IOException, NullPointerException {
}
}
class B extends A{
void method() {
}
}
class AA {
public static void main(String[] args) {
}
void method() {
}
}
}
the answer for a question said that the method can catch only unchecked exceptions. HOw is that possible can anyone explain.
Thanks in advance.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is fine; what is it that you don't understand? When a method overrides another method, it is free to change the exception signature to make it more restrictive (it can't add new exceptions, but it can take some away.)
The class AA has nothing whatsoever to do with the other two classes, so I'm not sure why you've included it, although there seems to be an extra close-brace at the end, which may indicate that something is missing.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine there are two exception classes called Exception1 and Exception2 that descend from the Exception class. Given these two class definitions:
class First {
void test() throws Exception1, Exception2 { . . . }
}

class Second extends First {
void test() { . . . }
}
Create a class called Third that extends Second and defines a test() method. What exceptions can Third’s test() method throw?
a.Exception1
b.Exception2
c.no checked exceptions
d.any exceptions declared in the throws clause of the Third’s test() method

The answer to the above question is said as "C". But i feel the answer should be "a & b". I was intending to ask this question. I am extremely sorry for the above mistake. can you please explain why only c is correct and why not a and b.
Thanks for the reply.
 
Amit KumarS
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hiii..
i think it is pretty simple... the rule says that you cannot add new exceptions to method while overriding from parent class. and in this case the parent class of third class is second class and not the First Class..
so according to rules, one cannot add new exceptions.. so, as the method in Second class doesnot throw any exception... the dervied class also cannot throw any exception..
Thanks
Amit
 
Chandra Bairi
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean that the method in class Third cannot throw the exceptions exception1 and exception2 but can throw any other exceptions.
One doubt, because I have the method in class Second overriding a method in class First will the original behaviour of the method in class First completely lost.
if that is the case then the method in class Second should be able to throw any exceptions because it is just like any other method in the class Second.
(or)
but if the original behaviour of the method of Class First is not lost in class Second even after overriding then the method in class Second should be able to throw the same exceptions and if it can throw the exceptions then any classes subclassing that class should be able to throw it.
Any comments will help me. Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, if you override a method, you change the exception declaration to make it more restrictive, but not less; i.e., you can throw fewer exceptions, but not more. So if Second declares that it throws no exceptions, then Third must also declare that it throws no exceptions.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic