error related to exception handling in java

Viewed 85

How to resolve this error? I have tried using throws to throw FileNotFoundException but still same error.

Compile-Time Error : "Default constructor cannot handle exception type Exception thrown by the implicit super constructor. Must define an explicit constructor "

CODE :

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileOne {
    
    Scanner sc = new Scanner(System.in);
    String file_name = sc.nextLine();
    File obj = new File(file_name);
    Scanner reader_obj = new Scanner(obj); // <--- error in this line
    
    public static void main(String args[]) {
        FileOne f = new FileOne();
        f.create();
        f.writeFile();
        f.readFile();

    }
    

    void create() {
        try {
            System.out.println("Enter a file name");

            if (obj.createNewFile()) {
                System.out.println("file name is" + obj.getName());
            } else {
                System.out.println("Already exists");
            }
        } catch (IOException e) {
            System.out.println("error occured while creating");
        }
    }

    //method to write in file
    void writeFile() {
        try {
            FileWriter w = new FileWriter(obj);
            w.write("Learning files now");
            w.close();
        } catch (IOException e) {
            System.out.println("Exception occured while writing a file");
        }
    }

    //method to read
    /* use the Scanner class to read the contents of the text file created */
    void readFile() {

        while (reader_obj.hasNextLine()) {
            String data = reader_obj.nextLine();
            System.out.println(data);
        }
        reader_obj.close();
    }
    
}
3 Answers

The line Scanner reader_obj=new Scanner(obj);, which is implicitly called by the default constructor, may throw a FileNotFoundException, which is a checked exception and must be handled.

One way of doing so is explicitly defining a no-arg constructor:

public FileOne() throws FileNotFoundException {
}

Although, if you're going to do that, you should consider moving the members' initialization in to it for clarity's sake.

Errors Resolved:

  1. I used throws to throw exceptions in main() and reading() method.
  2. Used FileReader class to read the data from the given input file

Final Code:

public class FileOne {
Scanner sc=new Scanner(System.in);
String file_name=sc.nextLine();
 File obj=new File(file_name);


//method for creating a file
void create(){
    try{
 
    
    if(obj.createNewFile()){
      System.out.println("file name is"+obj.getName());
    }
    else{
        System.out.println("Already exists");
    }
}
    catch(IOException e){
        System.out.println("error occured while creating");
    }    
}
//method to write in file
void writeFile(){
 try{
     FileWriter w=new FileWriter(obj);
     w.write("Learning files now");
     w.close();
 }
 catch(IOException e){
     System.out.println("Exception occured while writing a file");
 }
}
void reading() throws FileNotFoundException,IOException{
    FileReader reader=new FileReader(file_name);
    int i;
    while((i=reader.read())!=-1){
        System.out.print((char)i);
    }
    reader.close();
}
public static void main(String args[])throws FileNotFoundException,IOException{
   FileOne f=new FileOne();
   f.create(); 
   f.writeFile();
   f.reading();  
}
}

Add the constructor as below :

public FileOne () throws FileNotFoundException {
    
}

Edit your void main () as below (You need to throw the exception from main as well) :

public static void main(String args[]) throws FileNotFoundException {
        FileOne f = new FileOne();
        f.create();
        f.writeFile();
        f.readFile();

}
Related