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.
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.
If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent 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 { // ... } }
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.
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.
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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With