I am writing a class in Java, which is a subclass of another class that I wrote, and its constructor explicitly calls the constructor of the superclass. A superclass constructor can throw several types of exceptions during initialization directly, however, when initializing an instance of my subclass, there are several exceptions that will never be thrown (by design).
I tried to catch these exceptions in the constructor of the subclass, but I got an error stating that "calling the constructor should be the first statement in the constructor." Why can't I take advantage of 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) { } } }
notes super(file); error super(file); like a mistake.
How can I implement a subclass constructor so that it knows that these exceptions do not matter? I need this because I do not want to wrap this constructor in try {} ... catch {} for each exception in my code later.
source share