When building a default constructor, it is not possible to handle an exception: type Exception thrown by an implicit super constructor

The code works fine until I try to turn the code into a constructive class. When I try to build an object from it, I get an error

"The default constructor cannot handle the type of IOException thrown by the implicit superconstructor. You must define an explicit constructor"

This is when you need to throw exceptions in FileReader and BufferedReader .

thanks

EDIT:

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

xFile obtained from the construction. Note that this design throws exceptions.

+7
source share
4 answers

The default constructor implicitly calls the super constructor, which is supposed to throw some kind of exception that needs to be handled in the constructor of the subclass. write a code for a detailed answer

 class Base{ public Base() throw SomeException{ //some code } } class Child extends Base{ public Child(){ //here it implicitly invokes `Base()`, So handle it here } } 
+6
source

The base class super.constructor is implicitly called by the extended constructor of the class:

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

Now you need to handle the exception inside Derived() or make the constructor like,

 public Derived() throws Exception { } 

Whatever way you new place the Derived object, either enclose it in a try-catch , or make this method throw an Exception , as described above. [Note: this is a pseudo code]

+3
source

Any subclass that extends the superclass, the default constructor handles some kind of exception, the subclass must have a default constructor that implements the exception

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

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

0
source

Just attach your call to the constructor in Try-Catch.

0
source

Source: https://habr.com/ru/post/893237/


All Articles