I have a problem compiling my code, I'm trying to make a class method throw a personalized exception, given some conditions. But at compile time, I get a message:
Overridden method does not throw an exception
Here's the class declaration and exceptions:
public class UNGraph implements Graph
Graph is an interface with all UNGraph methods in it (the getId() method does not have a throws declaration on this script)
After the constructor, I throw an exception (inside the UNGraph class):
public class NoSuchElementException extends Exception { public NoSuchElementException(String message){ super(message); } }
Here is an exception method
public int getId(....) throws NoSuchElementException { if (condition is met) { //Do method return variable; } else{ throw new NoSuchElementException (message); } }
Obviously, I do not want the method to throw an exception each time only when the condition is not met; and when he meets, I want to return a variable.
java methods method-overriding exception
user3613551
source share