Overridden method does not throw an exception

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.

+7
java methods method-overriding exception
source share
3 answers

The compiler throws an error because Java does not allow you to override the method and add a checked exception (any custom exception that extends the Exception class). Since it is clear that you want to handle the script when a condition is not met as an unexpected event (error), the best option is to throw a RuntimeException . A RuntimeException , for example: IllegalArgumentException or NullPointerException , does not have to be included in the method signature, so you will get rid of a compiler error.

I suggest the following changes to your code:

 //First: Change the base class exception to RuntimeException: public class NoSuchElementException extends RuntimeException { public NoSuchElementException(String message){ super(message); } } //Second: Remove the exception clause of the getId signature //(and remove the unnecessary else structure): public int getId(....) { if ( condition is met) { return variable; } //Exception will only be thrown if condition is not met: throw new NoSuchElementException (message); } 
+10
source share

The problem becomes clear when you have code like this using your class and interface:

 Graph g = new UNGraph(); int id = g.getId(...); 

The Graph interface does not declare that it has thrown a NoSuchElementException , so the compiler would allow this code without a try or throws block for any method this code is in. the override method can explicitly throw a checked exception; he announced as much. For this reason, the overriding method cannot raise more checked exceptions than the overridden or abstract method. There would be a difference in how the calling code should handle checked exceptions, depending on the actual type of object.

Ask the interface method declaration to declare that it throws a NoSuchElementException or has a method of the implementation class that handles the NoSuchElementException itself.

0
source share

You must declare a throws NoSuchElementException in all superclasses for a checked exception if you want subclasses to throw a checked exception in this method.

Read more about Java Language Specification :

11.2. Check compilation exception time

The Java programming language requires the program to contain handlers for checked exceptions that may occur as a result of executing a method or constructor. For each checked exception, which is a possible result, the throws clause for the method ( ยง8.4.6 ) or the constructor ( ยง8.8.5 ) must indicate the class of this exception or one of the superclasses of the class of this exception ( ยง11.2.3 ).

This check of compilation time for exception handlers is intended to reduce the number of exceptions that are not handled properly. The checked exception classes ( ยง11.1.1 ) named in the throws clause are part of the contract between the developer and the user of the method or constructor. The throws clause of the overriding method may not indicate that this method will throw any checked exception that the overridden method is not allowed by its throws clause to throw ( ยง8.4.8.3 ).


While I'm in, you probably shouldn't use a NoSuchElementException , as used in the JRE ... use a different name.

0
source share

All Articles