Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using exception handling to eliminate irrelevant exceptions in subclass constructor

Tags:

java

I'm writing a class in Java which is a subclass of another class I wrote, and its constructor explicitly calls the superclass's constructor. The constructor of the superclass may throw several types of exceptions when initialized directly, however when I initialize an instance of my subclass there are several exceptions that will never by thrown (by design).

I tried catching these exceptions in the subclass's constructor but I received an error stating that "Constructor call must be the first statement in a constructor". Why can't I catch these exceptions?

For example, the code below:

public class Persian_Cat extends Cat {

    public Persian_Cat(File file) {
        try{
            super(file);
        } catch(InvalidArgumentException e) {
        } catch(FileNotFoundException e) {
        }
    }
}

marks the statement super(file); as the error.

How can I implement the subclass constructor so that it will know these exceptions are irrelevant? I need this because I don't wish to wrap this constructor in try{}...catch{} for every exception in my code later.

like image 421
mittelmania Avatar asked Aug 26 '14 14:08

mittelmania


People also ask

Can we handle exceptions in constructor?

The short answer to the question “can a constructor throw an exception in Java” is yes! Of course, properly implementing exceptions in your constructors is essential to getting the best results and optimizing your code.

Can subclass overriding method declare an exception if parent class method doesn't throw an exception?

If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

Can we throw an unchecked exception in child class if parent class doesn't throw any exception?

When the parent class method has a throws clause with an unchecked exception, the child class method can throw none or any number of unchecked exceptions, even though they are not related. Here's an example that honors the rule: class Parent { void doSomething() throws IllegalArgumentException { // ... } }

Can a subclass throw an exception?

Subclass method cannot throw the exception which is a superclass of the superclass method's exception. Subclass method can throw any unchecked or runtime exception.

What does it mean when a constructor throws an exception?

It means the child class constructor is responsible for handling the exception thrown by the parent class constructor. Now, for handling an exception there are two ways, one is to catch the exception and another is to throw it. But in the case of the constructor, we can’t handle it using the try-catch mechanism.

What is the exception handling rule for superclass methods?

Rule 1: If the superclass method declares an exception, subclass overridden method can declare the same subclass exception or no exception but cannot declare parent exception. TestExceptionChild2.java

What happens if superclass method does not declare exception?

If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception. Rule 1: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.

How to display the value of an exception in a subclass?

This subclass is preferably simple. As it has only a constructor plus an overridden the method toString () that displays the value of the exception. The class JavaProgram defines a calculate () method which throws MyException object. The exception is thrown when the calculate () method's integer parameter is greater than 10.


1 Answers

Sadly, if you use super(...); it must be the very first line of code in the constructor. There is no way to avoid this.

One solution would be to create another constructor that doesn't throw those exceptions. It may be sensible to scope this as protected, rather than public. You would want to document the API to make it clear which input validation (or whatever) is not being conducted because of which assumptions.

like image 75
Duncan Jones Avatar answered Oct 24 '22 18:10

Duncan Jones