11

The code works fine until I try to make the code into a constructable class. When I attempt to construct an object from it I get the error

"Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor"

This is when having to throw exceptions to FileReader and BufferedReader.

Thanks

EDIT:

FileReader textFilethree = new FileReader (xFile);
BufferedReader bufferedTextthree = new BufferedReader (textFilethree) ;
String lineThree = bufferedTextthree.readLine();

The xFile is gotten from the construction. Note that within this construction exceptions are thrown.

5 Answers 5

6

Default constructor implicitly invokes super constructor which is assumed to be throwing some exception which you need to handle in sub class's constructor . for detailed answer post the code

class Base{

  public Base() throw SomeException{
    //some code
  }

}

class Child extends Base{
  public Child(){
   //here it implicitly invokes `Base()`, So handle it here
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

you need to add relevant construtor's
It just has the parameter for FileReader as a parameter of the constructor with exceptions thrown.
so you need to catch or throw it in sub class's constructor
Yea I did throw it, but then I got the error message in my IDE when I tried to construct.
again you need to handle it while constructing. it propogates
|
3

Base class super.constructor is implicitly invoked by the extending class constructor:

class Base
{
  public Base () throws Exception
  {
    throw <>;
  }
}

class Derived extends Base
{
  public Derived ()
  {
  }
}

Now, one need to handle the exception inside Derived() or make the constructor as,

public Derived() throws Exception
{
}

Whatever method you new up the object of Derived, either you enclose it in try-catch or make that method throwing Exception as above. [Note: this is pseudo code]

1 Comment

I'm thinking of erring on forgetting about the ide error, I ran it and it was fine. Will I run into problems later if I do this?
2

Just enclose your call to the Constructor in a Try-Catch.

Comments

0

Any subclass which extends a super class whose default constructor handles some exception, a subclass must have a default constructor which implements the exception

class Super{ public Super() throws Exception {}}

class Sub extends Super{public Sub()throws Exception{//}}

Comments

0

if you have something like this

class example{
    public void fileReader(String path) throws Exception{
         //some code that throws Exception
    }
 }

Make sure to use that same sintaxis on the method you are trying to make the object

class implementation{
     public void someMethod() throws Exception{
          example object = new example();
          example.filereader("C:\\....");
      }
  }

happened to me while using apache poi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.