何时捕获异常与何时抛出异常? [英] When to catch the Exception vs When to throw the Exceptions?

查看:28
本文介绍了何时捕获异常与何时抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用 Java 编码有一段时间了.但有时,我不明白什么时候应该抛出异常,什么时候应该捕获异常.我正在做一个有很多方法的项目.层次结构是这样的-

I have been coding in Java for a while now. But sometimes, I don't understand when I should throw the exception and when should I catch the exception. I am working on a project in which there are lot of methods. The hierarchy is something like this-

Method A will call Method B and Method B will call some Method C and Method C will call Method D and Method E.

所以目前我正在做的是 - 我在所有方法中抛出异常并在方法 A 中捕获它,然后记录为错误.

So currently what I am doing is- I am throwing exceptions in all the methods and catching it in Method A and then logging as an error.

但我不确定这是否是正确的做法?或者我应该开始在所有方法中捕获异常.所以这就是为什么这种混乱始于我的 - 我什么时候应该捕获异常与什么时候应该抛出异常.我知道这是一个愚蠢的问题,但不知何故我很难理解这个主要概念.

But I am not sure whether this will be the right way to do it? Or should I start catching exceptions in all the Methods. So that is why this confusion started in my- When should I catch the Exception vs When should I throw the exceptions. I know it's a silly question but somehow I am struggling to understand this major concept.

谁能给我一个关于何时捕获异常与何时抛出异常的详细示例,以便我的概念在这方面得到澄清?就我而言,我应该继续抛出异常,然后在主调用方法 A 中捕获它吗?

Can someone give me a detailed example of When to catch the Exception vs When to throw the Exceptions so that my concepts gets cleared on this? And in my case, should I keep on throwing the exception and then catch it in the main calling Method A?

推荐答案

当你在知道该做什么的方法中时,你应该捕获异常.

You should catch the exception when you are in the method that knows what to do.

例如,暂时忘记它的实际工作原理,假设您正在编写一个用于打开和读取文件的库.

For example, forget about how it actually works for the moment, let's say you are writing a library for opening and reading files.

所以你有一堂课,比如说:

So you have a class, say:

public class FileInputStream extends InputStream {
    public FileInputStream(String filename) { }
}

现在,假设文件不存在.你该怎么办?如果您在苦苦思索答案,那是因为没有答案……FileInputStream 不知道如何处理该问题.所以它把它扔到链上,即:

Now, lets say the file doesn't exist. What should you do? If you're struggling to think of the answer, that's because there isn't one... the FileInputStream doesn't know what to do about that problem. So it throws it up the chain, i.e.:

public class FileInputStream extends InputStream {
    public FileInputStream(String filename) throws FileNotFoundException { }
}

现在,假设有人在使用您的图书馆.他们的代码可能如下所示:

Now, lets say someone's using your library. They might have code that looks like this:

public class Main {
    public static void main(String... args) {
        String filename = "foo.txt";
        try {
            FileInputStream fs = new FileInputStream(filename);

            // The rest of the code
        } catch (FileNotFoundException e) {
            System.err.println("Unable to find input file: " + filename);
            System.err.println("Terminating...");
            System.exit(3);
        }
    }
}

在这里,程序员知道该做什么,所以他们捕获异常并处理它.

Here, the programmer knows what to do, so they catch the exception and handle it.

这篇关于何时捕获异常与何时抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆