我应该故意捕获检查异常吗?

发布于 2024-11-09 16:16:13 字数 1505 浏览 0 评论 0原文

我设计了一个上传文件处理如下:

UploadFileHandler 是提供检查方法的主类。

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.
    }
}

UploadedFileChecker 进行检查。

public class UploadedFileChecker {
    private String fileName;

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

BadUploadedFileException 声明如下:

public class BadUploadedFileException extends RuntimException {

}

我让它扩展 RuntimeException,因为它使 UploadedFileChecker 中的代码变得干净,但这样做会使其成为未经检查的异常。因此,handleUploadedFile 中的 catch 是不合适的,因为我们不应该捕获未经检查的异常。

我的问题是,我应该捕获 BadUploadedFileException 还是使其扩展 Exception 并将“抛出 BadUploadedFileException”附加到 UploadedFileChecker 的每个方法。

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(String 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谜泪 2024-11-16 16:16:14

我认为你最好让 check() 返回一个布尔值。
我相信这会让代码看起来更干净,就像 Brian 所说的那样——异常并不是真正用于逻辑的——它们是在出现意外错误时出现的。

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.

原野 2024-11-16 16:16:14

如果你遇到例外,你会采取什么措施吗?如果没有,就让它冒泡。如果是,那么您当然可以捕获未检查异常。

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.

年少掌心 2024-11-16 16:16:14

就我个人而言,我认为应该是受检查的异常。你应该概率。从每个文件处理方法中抛出它并在 UploadedFileHandler 中捕获它,适当地处理它,如果您需要不同的处理逻辑,则将 BadUploadedFileException 拆分为许多不同类型的异常。

有关检查和非检查异常的更多信息请参见此处

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.

合久必婚 2024-11-16 16:16:14

最后一个选择会更好。我不认为这会使代码更难阅读,至少您(以及即将推出的版本中的同事)会记住处理它。

除此之外,当然不禁止捕获从 RuntimeException 派生的异常。另一方面,捕获 RuntimeException 或 Exception 只能在 main 方法等中完成(它可能是 OutOfMemoryException!)。

如果您使用显式 RuntimeExceptions,请不要在方法规范中使用“throws”关键字,但如果它们看起来很重要,请将它们包含在 JavaDoc 中。

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.

断桥再见 2024-11-16 16:16:13

异常应该用于异常条件。你意想不到的事情会发生。

您不应该将它们用于条件逻辑。

Josh Bloch 在他的书中具体概述了这一点,这本书相当不错,恕我直言,这是一本必备的书:

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

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/

鱼忆七猫命九 2024-11-16 16:16:13
since we should not catch unchecked exceptions.

没有这样的规则。只要您知道如何处理任何异常,您就可以捕获它。

然而,决不能发现错误!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文