• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding a method that throws an Exception.  RSS feed

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding a method that throws an Exception.
I learned that when I override getX in class B I am not allowed
To throw an exception which is not known the exception or a superclass
Of the exception thrown by the baseclass.
It would be possible to say:
public void getX ( ) throws java . io . FileNotFoundException { }
but not possible to say
public void getX ( ) throws Exception { }

But why is it possible to say:
public void getX ( ) throws RuntimeException { }
???
[CODE]
class A {
public void getX ( ) throws java.io.IOException { }
}
class B extends A {
public void getX ( ) throws Exception { }
// It would be possible to override getX with
//public void getX ( ) throws java . io . FileNotFoundException { }
//because FileNotFoundException is a class or subclass of IOException
}

public class Test91 {
public static final void main(String args[]) {
A a = new A();
B b = new B();

}
}
[CODE]
This doesn’t compile as subclass throws excecption which is not class or
Subclass of the IOException thrown by base class.
But why is it possible to say:
public void getX ( ) throws RuntimeException { }
[CODE]
class A {
public void getX ( ) throws java.io.IOException { }
}
class B extends A {
public void getX ( ) throws RuntimeException { }
}

public class Test91 {
public static final void main(String args[]) {
A a = new A();
B b = new B();

}
}
[CODE]
This code compiles but why?
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"If I override getX in class B, I am not allowed
to throw an exception which is not known the exception or a superclass of the exception thrown by the baseclass."

RuntimeExceptions are exempt from the rule. They are an exception to the Exception override rule.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marilyn de Queiroz:
"If I override getX in class B, I am not allowed
to throw an exception which is not known the exception or a superclass of the exception thrown by the baseclass."

RuntimeExceptions are exempt from the rule. They are an exception to the Exception override rule.

And that is because *any* method is allowed to throw RuntimeExceptions (so you could think of any method to implicitely declare throwing a RTE).
 
Pierre Post
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, a method can throw runtime exceptions without declaring them in the method signature.
Thus,

would successfully compile.
Pierre
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

not possible to say
public void getX ( ) throws Exception { }
But why is it possible to say:
public void getX ( ) throws RuntimeException { }

The reasons you can throw RuntimeException and not Exception is because RuntimeException (and all its subclasses) are known as unchecked exceptions. That bascially means that at compile time you don't have to specify that it is going to be thrown. In the method signature you don't even have to specify that you are throwing it. Since its not checked the methods that call it don't have to put the calling code in a try..catch block or throw it. If it happens it happens. It is often good practice to specify that it is going to be thrown because of code clarity so its easier to understand and maintain.
Subclasses of Exception (not including isn't RuntimeException) are called checked exceptions and you do have to specify at runtime that a method can throw it so all methods that call it either have to put it in a try..catch block or throw it up to the caller. Since your interface doesn't specify that Exception will be thrown it is confusing if subclasses throw it because client code won't be expecting it because it is not specified in the interface, so thats why you can't throw it. Its similar to the idea that you can't decrease the access modifiers on methods in implementing interfaces. You have to abid by the contract that is given in the interface otherwise you can't implement it. It wouldn't be a bad idea if you just subclassed RuntimeException and just threw that exception, then if necessary you could just try to catch it in the calling methods. But this is going to be hard to enfore since you can't throw a checked exception. What are your other options?
 
saager mhatre
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Infact, not just runtin\me Exception and its subclasses, but, all Exceptions declared in java.lang are unchecked.
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not true, Saager. Only classes that extend java.lang.RuntimeException are unchecked. Classes like java.lang.ClassNotFoundException and java.lang.IllegalAccessException, for example, extend java.lang.Exception directly and are checked exceptions, not to mention that java.lang.Exception, itself, is a checked exception.
[ November 30, 2002: Message edited by: Marilyn de Queiroz ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!