File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Usage of Exceptions with Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Usage of Exceptions with Overriding" Watch "Usage of Exceptions with Overriding" New topic
Author

Usage of Exceptions with Overriding

Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
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

Joined: Dec 28, 2006
Posts: 893
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 (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
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

Joined: Nov 08, 2006
Posts: 71
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
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

    Joined: Nov 08, 2006
    Posts: 71
    Hi Friends,
    Thanks for your good explanation. It is clear now.
    Friedman, the explanation given by you is very clear.

    -Ravindra.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Usage of Exceptions with Overriding