0

I am having the following issue above.

I have tried actually putting a try-catch statement into the code as you will see below, but I can't get the compiler to get past that.

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

Could someone tell me why I'm getting this issue?

  • 2
    http://stackoverflow.com/questions/9734051/why-do-i-keep-getting-the-must-be-caught-or-declared-to-be-thrown-error?rq=1 , http://stackoverflow.com/questions/15318621/unreported-exception-filenotfoundexception-must-be-caught-or-declared-to-be-thr?rq=1 , http://stackoverflow.com/questions/16498923/unreported-exception-java-io-filenotfoundexception-must-be-caught-or-declared-t?rq=1 http://en.wikipedia.org/wiki/Exception_handling#Checked_exceptions – user2864740 Oct 28 '13 at 22:03

2 Answers2

0

In processFile() you create DirectoryStatistics instance. In DirectoryStatistics constructor you declared FileNotFoundException. So when you try to istantiate DirectoryStatistics you should either handle this exception, or declare it in method signature. This is the rule for checked exceptions.

Admit
  • 4,897
  • 4
  • 18
  • 26
0

I presume that the compiler is complaining at statements where you are instantiating either DirectoryStatistics or DirectorySize.

The problem is this. You have declared that the DirectoryStatistics and DirectorySize may throw FileNotFoundException; e.g.

 public DirectoryStatistics(File startingDirectory) 
     throws FileNotFoundException

 public DirectorySize(File startingDirectory) 
     throws FileNotFoundException

Since you have declared that, and since it is a checked exception, you are required to "deal with" that exception in the constructor.

In your DirectoryStatistics constructor you have attempted to handle the exception. However that is not sufficient.

  • The super call in DirectoryStatistics constructor is calling the DirectorySize constructor.

  • The super constructor also throws that declaration.

  • The super call is not inside the try / catch.

  • You cannot put it there because the Java syntax rules don't allow it. The explicit (or implicit) super call must be the first statement of a constructor.

  • Even if you did, the fact that you've declared the DirectoryStatistics constructor as throwing the exception means that the caller of that constructor has to deal with it. It is not relevant that (this version of) the constructor might not allow the exception to propagate.


In this particular example, you may be able to "fix" this by removing the throws on both constructors. But that assumes that the DirectoryProcessor constructor doesn't also throw the exception.

In general, if a superclass constructor throws checked exception, then any subclass constructor that chains that constructor has no choice but to also throws the same exception ... or a superclass of the exception. (And if you think about this in terms of object encapsulation, it is a good thing that Java works this way.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Ok. I've also done this with other methods I needed to. I still have one problem though. It's not properly calculating the number of files and total file size of a directory. It's just showing 0's for everything. – Dan Czarnecki Oct 29 '13 at 16:33
  • Thanks again for the help! Turns out I missed doing some other things like making the condition in my first if statement greater than 0, as opposed to equal to (==) 0. Now my program works perfectly. – Dan Czarnecki Oct 30 '13 at 02:55