While constructing the default constructor can not handle exception : type Exception thrown by implicit super constructor

Viewed 46891

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

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

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

Related