java - Overriden method does not throw -


i'm having errors here. cant quite figure out. hmmm.

class myexception extends exception {      interface myinterface {         void mymethod () throws myexception;     }      class myimplementation implements myinterface {          @override         public void mymethod() throws myexception {             system.out.println("in mymethod()");             throw new myexception();         }     }      class theinterface { }      public static void main(string[] args) {         myimplementation m;         m = new myimplementation();         try {             m.mymethod();         } catch (myexception e) {             system.out.println("myexception caught");         }     } } 

please help.

you using user defined exception , have not created that. try or use generic exception

public class myimplementation implements myinterface {      @override     public void mymethod() throws myexception {         system.out.println("in mymethod()");         throw new myexception();     }  }  interface myinterface {     void mymethod () throws myexception; }  public class myexception extends exception {  }  public class testthrowsexception {      public static void main(string[] args) {         myimplementation m = new myimplementation();          try {             m.mymethod();          }           catch (myexception e) {             system.out.println("myexception caught");          }     } } 

by test way:-

public class testthrowsexception {     @test(expected = myexception.class)     public void shoudldthrowexception() throws myexception {          myimplementation m = new myimplementation();          m.mymethod();     } } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -