28

Can anyone tell me what reasons exceptions can have, not to be compatible with "throws" clauses

For instance:

class Sub extends Super{

    @Override
    void foo() throws Exception{

    }

}

class Super{

    void foo() throws IOException{

    }
}

Exception Exception is not compatible with throws clause in Super.foo()

6
  • 2
    Please show us the complete code demonstrating the problem. Including also the fully qualified class name for your Exception(s). May 9, 2012 at 9:34
  • 2
    can you show a bit more of your code and the exact error you are getting?
    – aniri
    May 9, 2012 at 9:35
  • Mine works. You should show us more code than that.
    – Averroes
    May 9, 2012 at 9:37
  • I'm sorry but since that's NOT my Homework this is quite hard to manage May 9, 2012 at 9:41
  • @Franz, we aren't expecting you to publish closed source production code here. But surely you can strip the code in question to the bare minimum required to reproduce the problem, and change class/variable names as necessary, to remove any company/project specific traces from it. May 9, 2012 at 9:47

4 Answers 4

33

Without a full code sample, I can only guess: you are overriding/implementing a method in a subclass, but the exception specification of the subclass method is not compatible with (i.e. not a subset of) that of the superclass/interface method?

This can happen if the base method is declared to throw no exceptions at all, or e.g. java.io.IOException (which is a subclass of java.lang.Exception your method is trying to throw here). Clients of the base class/interface expect its instances to adhere to the contract declared by the base method, so throwing Exception from an implementation of that method would break the contract (and LSP).

5
  • Thank you Péter Török- Issue closed May 9, 2012 at 9:43
  • I know that's not a discussion site- but i don't understand why LSP not allows to add something..!? May 9, 2012 at 9:49
  • 7
    It allows adding something. But when you're adding an exception type to an overridden method, you're not adding something, but removing something: with the base class, you're guaranteed to never get an exception, but with the subclass, you lose this guarantee. That's why it's forbidden.
    – JB Nizet
    May 9, 2012 at 9:57
  • I see! Thanks for this explanation. I thought checked exceptions where more something like a prospect not a guarantee. May 9, 2012 at 10:28
  • So the workaround is to use try-catch in implementing class? I have been given some interfaces which I have to implement in my implementation. In my implementation, I have to use libraries which throw some weird exceptions. Should I use try-catch in my implementation or its more wise for interface provider to define methods throw Exception?
    – Mahesha999
    Jun 5, 2017 at 10:07
0

To fix it use RuntimeException

public T findById(long id) throws RuntimeException {
    try {
          return whatEver.create();
    } catch (SystemException e) {
          throw new RuntimeException(e);
    }
}

Hope this helps.

-1

Checked exceptions are intended for use in situations where a method may expect its caller to be prepared to deal with certain problems that may arise. If callers of BaseFoo.Bar() are not required to deal with a FnordException, then method DerivedFoo.Bar() cannot expect its callers to deal with a FnordException either (since many of its callers will be the same ones that were unprepared to have BaseFoo.Bar() throw it).

Conceptually, that's great. In practice, not so much. The problem is that the design of checked exceptions in the language assumes that either no callers will be prepared to deal gracefully with a particular problem, or all callers will be prepared to deal with it. The normal state of affairs in practice is for callers to be unprepared to deal with exceptions--even those that some callers might be able to handle. Most of the time, the proper course of action when code receives a checked exception it's not explicitly expecting would be to wrap it in an unchecked exception and throw that. Ironically, the easiest course of action--adding a "throws" clause and letting the checked exception bubble up, is probably the one which is least likely to be correct. While there are a few cases (e.g. IOException) where such behavior would make sense (e.g. when trying to read a collection from a file, an I/O error while reading one item is an I/O error while reading the collection), an exception thrown from within a nested method call will represent a different condition than will an exception of the same type thrown by the outer method, and code which would be prepared to handle the latter may not be prepared to handle the former.

In your situation, your best bet may be to catch IOException and wrap it in some other type that derives from RuntimeException, being aware that your callers are unlikely to be able to handle it.

-1

Make sure you declared throw in your interface. If you did, but the problem still persists - try saving/rebuilding the project.

1
  • i don't think this is the way to go.
    – wake-0
    Jun 29, 2016 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.