Franz Ebner Franz Ebner - 3 months ago 6x
Java Question

What are reasons for Exceptions not to be compatible with throws clauses?

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

For instance:

class Sub extends Super{

void foo() throws Exception{



class Super{

void foo() throws IOException{


Exception Exception is not compatible with throws clause in


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