Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overridden method does not throw exception

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.

like image 723
user3613551 Avatar asked May 07 '14 19:05

user3613551


People also ask

Can we throw exception in overridden method?

An overriding method can throw any unchecked exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method.

Can we override a method that throws runtime exception without throws clause?

According to the 3rd rule, if the super-class method throws certain exception, you can override it without throwing any exception.

What happens when you override a method?

Instance Methods The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.

What are the rules need to follow when overriding a method that throws an exception in Java?

We need to follow some rules when we overriding a method that throws an Exception. When the parent class method doesn't throw any exceptions, the child class method can't throw any checked exception, but it may throw any unchecked exceptions.


1 Answers

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 a RuntimeException. A RuntimeException, such as: IllegalArgumentException or NullPointerException, 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:

//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);
}
like image 140
Sean Mickey Avatar answered Sep 24 '22 03:09

Sean Mickey