aspose file tools*
The moose likes Beginning Java and the fly likes exception while overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception while overriding" Watch "exception while overriding" New topic
Author

exception while overriding

suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
hi,please resolve the problem of exception for overriding.Actually ,if super class declares a checked exception,then the overriding method cannot declare a new exception,or a cheked exception of broader type.

But the following gives error;
class A
{
void method() throws Exception
{

}

}

class B extends A
{
void method()
{

}

}
In the above scenario,the overriding method cannot declare a new exception or a checked exception of broader type ,But it is showing the error.Please solve this problem.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Nothing wrong with this code; it would compile fine. Have you remembered to PostRealCode?


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your description rings true but there was something missing from your example. Let's say you wrote class A:

And I started using it:

You told me to handle IOException and I did. What if the compiler let somebody write a new class B:

And what if somebody passed an instance of B into my method? I'd be totally unprepared to handle a SQLException because A never told me it might happen. That would be bad enough that the compiler just doesn't let you write B that way.

There's a neat guideline called the Liskov Substition Principle that says if B extends A then any code that works with an instance of A should work equally well with an instance of B. Throwing unexpected exceptions would certainly break that rule!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception while overriding