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