0

上記の次の問題があります。

以下に示すように、コードに 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();
        }

この問題が発生する理由を誰かに教えてもらえますか?

4

2 に答える 2

0

インスタンスprocessFile()を作成します。コンストラクターで宣言しDirectoryStatisticsました。したがって、インスタンス化しようとするときは、この例外を処理するか、メソッド シグネチャで宣言する必要があります。これがチェック例外のルールです。DirectoryStatisticsFileNotFoundExceptionDirectoryStatistics

于 2013-10-28T21:57:11.783 に答える
0

DirectoryStatisticsまたはのいずれかをインスタンス化しているステートメントで、コンパイラが不平を言っていると思いますDirectorySize

問題はこれです。and が;をスローする可能性があると宣言しました。例えばDirectoryStatisticsDirectorySize FileNotFoundException

 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 がこのように機能するのは良いことです。)

于 2013-10-28T22:39:25.507 に答える