I have a problem when compiling my code, I'm trying to make a method of a class throw an personalized exception, given some conditions. But at the time of compiling I get the message:
Overridden method does not throw exception
Here's the class and exception declaration:
public class UNGraph implements Graph
Graph
is an interface with all the methods of UNGraph
in it (the method getId()
doesn't have the throws
declaration on that script)
After the constructor I create the exception (inside the class UNGraph):
public class NoSuchElementException extends Exception {
public NoSuchElementException(String message){
super(message);
}
}
Here is the method with the exception
public int getId(....) throws NoSuchElementException {
if (condition is met) {
//Do method
return variable;
}
else{
throw new NoSuchElementException (message);
}
}
Obviously I don't want the method to throw an exception every time, just when the condition is not met; and when it's met, I want to return a variable.
The compiler is issuing an error because Java does not allow you to override a method and add a checked Exception (any user-defined custom exception that extends the
Exception
class). Because it is clear that you want to handle the scenario where some condition is not met as an unexpected occurrence (a bug), your best option is to throw aRuntimeException
. ARuntimeException
, such as:IllegalArgumentException
orNullPointerException
, does not have to be included in a method signature, so you will alleviate your compiler error.I suggest the following changes to your code:
You have to declare the
throws NoSuchElementException
in all superclasses for a checked exception, if you want the subclasses to throw a checked exception in that method.You can read more in the Java Language Specification:
While I'm at it, you probably shouldn't use
NoSuchElementException
, as it's used in the JRE... use a different name.The problem becomes clear when you have such code as this using your class and interface:
The interface
Graph
doesn't declare that it throws the checked exceptionNoSuchElementException
, so the compiler would allow this code without atry
block or athrows
clause on whatever method this code is in. But the overriding method clearly can throw the checked exception; it has declared as much. This is the reason that an overriding method cannot throw more checked exceptions than an overridden or abstract method. There would be a difference in how the calling code needs to handle checked exceptions, depending on the actual type of the object.Have the interface's method declaration declare that it throws
NoSuchElementException
or have the implementing class's method handle theNoSuchElementException
itself.