我的IDE显示“未声明的FileNotFoundException必须被捕获或抛出”

时间:2013-10-28 21:51:32

标签: java filenotfoundexception

我上面遇到以下问题。

我已经尝试过将try-catch语句放入代码中,如下所示,但是我无法让编译器超越它。

import java.io.*;
public class DirectoryStatistics extends DirectorySize
{
    /*
    Dan Czarnecki
    October 24, 2013

    Class variables:
        private File directory
            A File object that holds the pathname of the directory to look in

        private long sizeInBytes
            A variable of type long that holds the size of a file/directory (in bytes)

        private long fileCount
            A variable of type long that holds the number of files in a directory


    Constructors:
        public DirectoryStatistics(File startingDirectory) throws FileNotFoundException
            Creates a DirectoryStatistics object, given a pathname (inherited from DirectorySize class),
            and has 3 instance variables that hold the directory to search in, the size of each file (in bytes),
            and the number of files within the directory

    Modification history:
        October 24, 2013
            Original version of class

    */
    private File directory;
    private long sizeInBytes;
    private long fileCount;

    public DirectoryStatistics(File startingDirectory) throws FileNotFoundException
    {
        super(startingDirectory);
        try
        {
            if(directory == null)
            {
                throw new IllegalArgumentException("null input");
            }
            if(directory.isDirectory() == false)
            {
                throw new FileNotFoundException("the following input is not a directory!");
            }
        }
        catch(IOException ioe)
        {
            System.out.println("You have not entered a directory.  Please try again.");
        }


    }

    public File getDirectory()
    {
        return this.directory;
    }

    public long getSizeInBytes()
    {
        return this.sizeInBytes;
    }

    public long getFileCount()
    {
        return this.fileCount;
    }

    public long setFileCount(long size)
    {
        fileCount = size;
        return size;
    }

    public long setSizeInBytes(long size)
    {
        sizeInBytes = size;
        return size;
    }

    public void incrementFileCount()
    {
        fileCount = fileCount + 1;
    }

    public void addToSizeInBytes(long addend)
    {
        sizeInBytes = sizeInBytes + addend;
    }

    public String toString()
    {
        return "Directory" + this.directory + "Size (in bytes) " + this.sizeInBytes + "Number of files: " + this.fileCount;
    }

    public int hashCode()
    {
        return this.directory.hashCode();
    }

    public boolean equals(DirectoryStatistics other)
    {
        return this.equals(other);
    }
}

import java.io.*;
import java.util.*;

public class DirectorySize extends DirectoryProcessor
{
    /*
    Dan Czarnecki
    October 17, 2013

    Class variables:
        private Vector<Long> directorySizeList
            Variable of type Vector<Long> that holds the total file size of files in that directory
            as well as files within folders of that directory

        private Vector<File> currentFile
            Variable of type Vector<File> that holds the parent directory

    Constructors:
        public DirectorySize(File startingDirectory) throws FileNotFoundException
            Creates a DirectorySize object, takes in a pathname (inherited from DirectoryProcessor class,
            and has a single vector of a DirectoryStatistics object to hold the files and folders
            within a directory

    Modification History
        October 17, 2013
            Original version of class
            Implemented run() and processFile() methods
    */
    private Vector<DirectoryStatistics> directory;

    /*
    private Vector<Long> directorySizeList;
    private Vector<File> currentFile;
    */

    public DirectorySize(File startingDirectory) throws FileNotFoundException
    {
        super(startingDirectory);
        directory = new Vector<DirectoryStatistics>();
    }


    public void processFile(File file)
    {
        DirectoryStatistics parent;
        int index;
        File parentFile;
        System.out.println(file.getName());
        System.out.println(file.getParent());

        parentFile = file.getParentFile();
        parent = new DirectoryStatistics(parentFile);
        System.out.println(parent);
        parent.equals(parent);
        index = directory.indexOf(parent);

        if(index == 0)
        {
            directory.elementAt(index).addToSizeInBytes(file.length());
            directory.elementAt(index).incrementFileCount();
        }

        if(index < 0)
        {
            directory.addElement(parent);
            directory.lastElement().setSizeInBytes(file.length());
            directory.lastElement().incrementFileCount();
        }

有人能告诉我为什么我会遇到这个问题吗?

2 个答案:

答案 0 :(得分:0)

processFile()中创建DirectoryStatistics个实例。在DirectoryStatistics构造函数中,您声明了FileNotFoundException。因此,当您尝试istantiate DirectoryStatistics时,您应该处理此异常,或者在方法签名中声明它。这是已检查例外的规则。

答案 1 :(得分:0)

我认为编译器在您正在实例化DirectoryStatisticsDirectorySize的语句中抱怨。

问题是这个。您已声明 DirectoryStatisticsDirectorySize 可能抛出FileNotFoundException; e.g。

 public DirectoryStatistics(File startingDirectory) 
     throws FileNotFoundException

 public DirectorySize(File startingDirectory) 
     throws FileNotFoundException

由于您已经声明了,并且由于它是一个经过检查的异常,因此您需要在构造函数中“处理”该异常。

DirectoryStatistics构造函数中,您尝试处理异常。但这还不够。

  • super构造函数中的DirectoryStatistics调用正在调用DirectorySize构造函数。

  • 超级构造函数抛出该声明。

  • try / catch内的超级电话

  • 无法将其放在那里,因为Java语法规则不允许它。显式(或隐式)超级调用必须是构造函数的第一个语句。

  • 即使你这样做了,你声明 DirectoryStatistics构造函数抛出异常的事实意味着该构造函数的调用者必须处理它。构造函数(此版本)可能不允许异常传播。


在此特定示例中,您可以通过删除两个构造函数上的throws来“修复”此问题。但是假设DirectoryProcessor构造函数也没有抛出异常。

通常,如果超类构造函数throws检查了异常,那么链接该构造函数的任何子类构造函数都没有选择但是throws同样的异常......或异常的超类。 (如果你从对象封装的角度考虑这个问题,Java就是这样做的好事。)