27

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()

Franz Ebner
  • 4,951
  • 3
  • 39
  • 57

4 Answers4

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).

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • Thank you Péter Török- Issue closed – Franz Ebner May 09 '12 at 09:43
  • I know that's not a discussion site- but i don't understand why LSP not allows to **add** something..!? – Franz Ebner May 09 '12 at 09: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 09 '12 at 09:57
  • I see! Thanks for this explanation. I thought checked exceptions where more something like a prospect not a guarantee. – Franz Ebner May 09 '12 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 05 '17 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.

Aalkhodiry
  • 2,178
  • 30
  • 25
-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.

supercat
  • 77,689
  • 9
  • 166
  • 211
-1

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