简体   繁体   English

与 java 中的异常处理相关的错误

[英]error related to exception handling in java

How to resolve this error?如何解决此错误? I have tried using throws to throw FileNotFoundException but still same error.我曾尝试使用throwsthrow FileNotFoundException但仍然是同样的错误。

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();
    }
    
}

The line Scanner reader_obj=new Scanner(obj);线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.由默认构造函数隐式调用,可能会抛出FileNotFoundException ,这是一个已检查的异常,必须进行处理。

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.我使用throwsmain()reading()方法中抛出异常。
  2. Used FileReader class to read the data from the given input file使用FileReader类从给定的输入文件中读取数据

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) :编辑您的void main ()如下(您还需要从 main 抛出异常):

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM