Home > Net >  Given the following snippet, would it be wise to specify the reason of the failure or throw an IOExc
Given the following snippet, would it be wise to specify the reason of the failure or throw an IOExc

Time:05-03

I'm creating a "File Manager" to practice my IO skills. If I'm creating a file, how would I handle an error if file#createNewFile() returns false?

@Override
    public void run() {
        File file = new File(src   "\\"   srcName);
        try {
            if (file.createNewFile()) {
                MessageHandler.createSuccess(comp);
            } else {
                throw new IOException();
            }
        } catch (IOException e) {
            MessageHandler.error500(comp);
        }
    }

Right now I'm simply throwing an IOException to be caught in the next block, but I'm not sure if that is the best idea. It should be fine on the client end but when debugging I would have no idea what went wrong internally.

CodePudding user response:

It depends. I guess there is no single clear answer to this, so other members of the community might view this differently than I do.

According to the Javadoc, createNewFile returns false only if the file already exists.

Therefore, in cases where the file should not be present whatsoever, I also just throw an IOException. If I have a reason to assume the file could be present, the user interacting with the program should be notified of this problem in some way. How exactly you do this depends on the UI of your program.

CodePudding user response:

The code snippet you showed us does not make much sense. Throwing an exception, just to catch it immediately, isn't very performant (unless the clever Java optimizers eliminate the costly operations).

Your current run() method does:

  1. MessageHandler.createSuccess() if the file didn't exist and could be created (file.createNewFile() returned true).
  2. MessageHandler.error500() if the file didn't exist, but could not be created, e.g. because of not having the necessary permissions (file.createNewFile() threw an IOException).
  3. MessageHandler.error500() if the file already existed (file.createNewFile() returned true). [Are you sure that already having the file is an error?]

For the case number 3, creating, throwing and catching an IOException does not serve any useful purpose (other than guiding the code into the catch clause).

I'd simply rewrite the code to something like

@Override
public void run() {
    File file = new File(src   "\\"   srcName);
    try {
        if (file.createNewFile()) {
            MessageHandler.createSuccess(comp);
        } else {
            MessageHandler.error500(comp);
        }
    } catch (IOException e) {
        MessageHandler.error500(comp);
    }
}

You might even find a result code different from 500, better representing the cause of the failure.

Exceptions are a very good way for one method to communicate to the other methods up its caller hierarchy that it wasn't able to do its job. Throwing and catching an exception within one method rarely is a good idea.

  • Related