未处理的异常类型FileNotFoundException

时间:2014-02-24 07:21:41

标签: java eclipse exception exception-handling filenotfoundexception

主要课程:

public class cfung58_lab03_Main {
//main class 
//creates objects

    //Unhandled Exception Type FileNotFoundException occurs on the line below
static cfung58_lab03_Department myDepartment = new cfung58_lab03_Department(); 

...    
}

系类:

public class cfung58_lab03_Department {
    ....
    static  cfung58_lab03_Course[] coursesArray; 
static cfung58_lab03_Student[] studentArray; 

    public cfung58_lab03_Department() throws FileNotFoundException{
      File file = new File("Courses.txt");  //file for Courses.txt
    //System.out.println(file.getCanonicalPath());//gets path
    //File file2 = new File("Students.txt"); //file for Students.txt
    //System.out.println("True or false: " + file.canRead()); 
    if (file.exists()){
        //if the course file exists 

        //load scanner object to read file course.txt string
                    //NOTE: remember to throw FileException 
        Scanner read = new Scanner(file); 

        //count the number of Courses first before 
                    // creating the coursesArray and filling in the elements 
        int numberOfCourses = 0; 

        while (read.hasNext()){
            //String courseName = read.next(); 
            numberOfCourses++; 
        }

        //create coursesArray
        cfung58_lab03_Course[] coursesArray = new cfung58_lab03_Course[numberOfCourses];

        //restart the scanner system for read
        read = new Scanner(file); 

        while (read.hasNext()){
            String courseName = read.next(); 

            //for loop to fill in the elements for coursesArray
            for (int i = 0 ; i < numberOfCourses ; i++){
                //creating element 
                coursesArray[i] = new cfung58_lab03_Course(courseName, 0 );
            }

        }
    }
    //else throw exception 
    else 
        throw new FileNotFoundException("File could not be found."); 

即使我已经在部门构造函数上抛出异常,我也不确定导致编译器错误的原因。

Eclipse也没有解决方案。

知道可能导致这种情况的原因吗?

4 个答案:

答案 0 :(得分:1)

您正在从可怕的命名主类中调用方法,并且此方法会抛出已检查的异常。因此,需要捕获(或抛出此异常,但由于您从方法中分配静态变量,因此无法将其抛出):

static cfung58_lab03_Department myDepartment;

static {
    try {
        myDepartment = new cfung58_lab03_Department(); 
    }
    catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
}

也就是说,这个字段应该是主方法的局部变量。

答案 1 :(得分:0)

删除“抛出FileNotFoundException”并使用“try&amp; catch”来获取错误。你将接近错误行。

答案 2 :(得分:0)

这里的解决方案是创建一个静态初始化块:

static cfung58_lab03_Department myDepartment;

static {
    try {
        myDepartment = new cfung58_lab03_Department();
    } catch (FileNotFoundException e) {
        throw new ExceptionInInitializerError(e);
    }
}

或者在Department构造函数中,抛出一个未经检查的异常。

答案 3 :(得分:0)

在您的代码cfung58_lab03_Department中,您在其他情况下抛出异常为..

//else throw exception 
else 
    throw new FileNotFoundException("File could not be found."); 

表示如果您的代码在上述路径中找不到文件,则默认会抛出异常。

尝试以下方法正确获取文件“Courses.txt”,

//使用适当的已知路径收集文件

文件文件=新文件(“resources \ file_path \ Courses.txt”);

//使用输入流收集文件

InputStream inputStream = this.getClass()。getClassLoader()。getResourceAsStream(“Courses.txt”);