0

I am currently trying to complete a project in BlueJ. I have a ReadWrite class and an InvalidFileException class.

I wish to call the toString() method from the InvalidFileException class to the ReadWrite class in the writeToFile() method.

The readWrite can only handle files ending in .txt. Once the boolean completes and finds a file that does not end in .txt it should return my custom exception class.

The writeToFile method checks the boolean if the file type exists; if not, it should return the InvalidFileException which I created.

I believe I need to call it after the else statement, but I have had no luck in figuring it out.

Here is my code.

ReadWrite class:

public boolean writeToFile() {
    boolean ok;

    try {
        FileWriter writer = new FileWriter(file);
        {
            if (file.toLowerCase().endsWith(".txt")) {
                ok = true;
                write();
            } else {
                ok = false;

            }
        }

    } catch (IOException e) {
        ok = false;
    }
}

InvalidFileExceptionClass *****

import java.io.FileNotFoundException;

public class InvalidFileException extends Exception {

    /**
     * Constructor for objects of class InvalidFileException
     */
    public InvalidFileException(String message) {
        super(message);
    }

    public String toString() {
        return ("Sorry but this system only accepts .txt files");
    }
}
3
  • 1
    Please edit your question. I am not sure what you are asking here. Commented Nov 19, 2015 at 22:56
  • 1
    Replace first ok=false with throw new YourException("your message"); No need to define toString() you can use your caught exception's getMessage function Commented Nov 19, 2015 at 23:01
  • Sorry spectacularbob, I have edited my question, The method should ensure that the file to be written to ends with the file extension ".txt". If not, my "InvalidFileException" should be thrown. I hope that makes some sense Commented Nov 19, 2015 at 23:06

2 Answers 2

3

There are a couple of issues at play here.

First, you do not "call" an exception; you throw one. That's easily accomplished with the throw keyword. The boolean is completely immaterial here.

Next, your custom exception extends Exception, which means it's checked. You either have to catch it yourself or declare it to be thrown. You don't want to catch it here, but you want the caller to deal with it instead. That's also easily accomplished with the throws keyword.

Lastly, you do not want to override the toString entirely, completely ignoring the message you pass in. It'd be a preferable design to incorporate the message provided in addition to yours, but I leave that decision to you.

How this might all look would be something like this:

public boolean writeToFile() throws InvalidFileException {
    try {
        FileWriter writer = new FileWriter(file);
        if (file.toLowerCase().endsWith(".txt")) {
            write();
        } else {
            throw new InvalidFileException("this message is completely ignored");
        }
    } catch (IOException e) {
        throw e; // something bad has happened and the exception should propagate
    }
}

Whatever's calling this method would have to wrap it in its own try...catch:

try {
    readWriteInstance.writeToFile();
} catch (InvalidFileException e) {
    System.out.println(e.toString());
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to call throw new InvalidFileException() when you detect the error, then catch that exception. There is no point in catching it immediately after you throw it, you might as well just print the error string there. You should catch it outside the writeToFile method, this allows you to handle the exception in the method that calls writeToFile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.