java - Can't override a method of parent class -


i compile error when trying override method of parent class:

public class reader {     public void readtodbfromfile(string filename, string table) throws filenotfoundexception() {          ....     } }  public class readerwithbackup extends reader {     @override     public void readtodbfromfile(string filename, string table) throws ioexception {         super.readtodbfromfile();         dobackup(filename, table);     }      ... } 

it says 'overriden method not throw ioexception'. mean? how can fix it?

thanks in advance.

the overridden method throws filenotfoundexception, overriding method must throw filenotfoundexception or sub-class of filenotfoundexception. ioexception super-class of filenotfoundexception, can't thrown overriding method.

consider code :

try {     reader reader = ...     reader.readtodbfromfile(..); } catch (filenotfoundexception ex) {     .. } 

this valid code, since readtodbfromfile method of reader can throw filenotfoundexception checked exceptions. if override readtodbfromfile sub-class of reader, code must still valid if assign instance of sub-class reader variable. therefore overriding method can't throw exception not covered throws clause of original method.


Comments