• 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

Overriding a method that throws an Exception.

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic