Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Usage of Exceptions with Overriding

 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

The below code compiles fine. Can you please tell why it wont throw any error as the two exceptions raised are not in hierarchy.
class A{
public void m1() throws ClassNotFoundException{
}
}
class B{
public void m1() throws RuntimeException{
}
}

The below code throws a compile time error. This is same as above code with altering the exception type.


class A{
public void m1() throws RuntimeException{
}
}
class B{
public void m1() throws ClassNotFoundException{
}
}
Please clarify.

Thanks,
Ravindra.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule is:

Overriden methods may only throw checked exceptions that are new or broader than these declared by the overridden method

That's why it doesn't compile.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule is:

Overriden methods may NOT !!! throw checked exceptions that are new or broader than these declared by the overridden method

That's why it doesn't compile.

Sorry for my typo.
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you mean to say if we override the unchecked exception in Super class, then the checked exception in subclass will take priority.

I have few more issues. Please have a look the following lines....

In the following code i am trying to overide ArithmeticException in the subclass with its super class RuntimeException. The below code compiles fine:

class A{
public void m1() throws ArithmeticException{}
}

class B extends A{
public void m1() throws RuntimeException{}
}


Here also i am trying to do the same for the checked exceptions.
The below code throws the compile time error.

class A{
public void m1() throws ClassNotFoundException{}
}

class B extends A{
public void m1() throws Exception{}
}

For the peer level classes in RuntimeException like ArithmeticException and NullPointerException i can able override one with the other which are of the same hierarchy.

But for the peer level classes in Exception like IOException and ClassNotFoundException i cannot able to override one with the other.

Can you please give the hierarchy rules that need to followed while using Exceptions with overriding.

Thanks,
Ravindra.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Under no circumstances does the compiler care about RuntimeException-related declarations. You can use a throws clause for them, but the compiler doesn't care. You can catch them or throw them, but the compiler doesn't care. Your declarations for them can conflict or disagree in inherited methods -- the compiler doesn't care. I guess this is what was confusing you above -- you're trying to figure out what the compiler allows with RuntimeExceptions. The answer is that it allows anything at all.

Now, for checked exceptions, the compiler does care. If you override a method with a throws clause, then your child method can either A) have no throws clause at all, or B) have the exact same throws clause, or C) have a throws clause that lists one or more subclasses of the exception type(s) thrown by the parent.
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule is that you must be able to invoke any method of the subclass exactly the same as a method of the superclass. If the superclass method doesn't declare a checked exception, then throwing a checked exception from the subclass means you would have to use a try-catch or a throws.You can invoke fooMethod like this:-but you can't invoke this sort of thing:-. . . without a try-catch.
As far as I can remember:-
  • It must be possible to call every subclass method the same way you call the superclass method.
  • If the superclass method "throws" a checked exception/exceptions the subclass method can declare it "throws" the same exceptions or their subclasses or fewer or no exceptions.
  • You can do anything you want with unchecked exceptions; the compiler doesn't worry about them.
  • The Bar.fooMethod declaration I showed you earlier won't compile because it declares a new checked exception.

    I think that is correct . . .

    The Java Language specification says it the other way round; it says the superclass "throws" must declare the same exception or their superclasses as the subclass "throws."
     
    Ravindranath Chowdary
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Friends,
    Thanks for your good explanation. It is clear now.
    Friedman, the explanation given by you is very clear.

    -Ravindra.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic