开发者

Should I catch checked exception intentionally?

开发者 https://www.devze.com 2023-03-08 12:18 出处:网络
I have designed a uploaded file handling as follow: The UploadFileHandler is the main class providing checking methods.

I have designed a uploaded file handling as follow:

The UploadFileHandler is the main class providing checking methods.

public class UploadedFileHandler {

    public static void handleUploadedFile(String fileName) {
        try {
            checkFile(fileName);
        } catch (BadUploadedFileException ex) {
            deleteFile(fileName);
        }
    }

    private static void checkFile(String fileName) {
        new UploadedFileChecker(fileName).check();
    }

    private static void deleteFile(String fileName) {
        //...code to delete the file.
    }
}

And the UploadedFileChecker do the checking.

public class UploadedFileChecker {
    private String fileName;

    public UploadedFileChecker(Stri开发者_如何学编程ng fileName) {
        this.fileName = fileName;
    }

    public void check() throws BadUploadedFileException {
        checkFileFormat();
        scanVirus();
    }

    private void checkFileFormat() {
       // if file format unsupported
       throw new BadUploadedFileException();
    }

    private void scanVirus() {
        // if contains virus
        throw new BadUploadedFileException();
    }
}

And the BadUploadedFileException is declared as follow:

public class BadUploadedFileException extends RuntimException {

}

I let it extend RuntimeException because it makes the code in UploadedFileChecker clean, but doing this make it an unchecked exception. Thus, the catch in handleUploadedFile is inapproriate since we should not catch unchecked exceptions.

My question is, should I catch the BadUploadedFileException or make it extends Exception and append "throws BadUploadedFileException" to every method of UploadedFileChecker.


Exceptions should be used for exceptional conditions. Things that you don't expect to happen.

You shouldn't use them for conditional logic.

Josh Bloch outlines that specifically in his book, which is quite good and a must-have IMHO:

http://java.sun.com/docs/books/effective/


since we should not catch unchecked exceptions.

There is no such rule. You catch any exceptions whenever you know what to do with it.

However, one must never catch Errors!


I think you'd be better off making check() return a boolean.
I believe that'll make the code look cleaner, and like Brian said- exceptions aren't really for logic- they're for when something goes unexpectedly wrong.


are you going to do anything if you get an exception? If not, let it bubble. If you are then you can certainly catch an uncheck exception.


Personally, I think it should be a checked exception. You should prob. throw it from each file handling method and catch it in UploadedFileHandler, handling it appropriately, if you need different handling logic then split BadUploadedFileException into a number of different types of exceptions.

More information on checked and unchecked exceptions here.


The last option would be preferable. I don't think it makes the code much harder to read, and at least you (and your colleagues in upcoming versions) will be remembering to handle it.

Besides that, it is certainly not forbidden to catch Exceptions derived from RuntimeException. Catching RuntimeException or Exception on the other hand should only be done in main methods and suchlike (it could be an OutOfMemoryException!).

If you use explicit RuntimeExceptions, don't use a "throws" keyword in the method specification but do include them in the JavaDoc if they seem important.

0

精彩评论

暂无评论...
验证码 换一张
取 消