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

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

问题描述

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

 方法A将调用方法B,方法B将调用一些方法C,方法C将调用方法D和方法E. 

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



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



有人可以给我一个详细的例子什么时候赶上异常vs何时抛出异常,以便我的概念被清除?在我的情况下,我应该继续抛出异常,然后在主调用方法A中找到它。

解决方案

当你在知道该怎么做的方法中时,可以捕获异常。



例如,忘记一下它实际上是如何工作的,假设你正在写一个库



所以你有一个课,说:

  public class FileInputStream扩展InputStream {
public FileInputStream(String filename){}
}

现在,让我们说文件不存在。你该怎么办?如果你正在努力想到这个答案,那是因为没有一个... FileInputStream 不知道该怎么做。所以它抛出了链,即:

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

现在,让我们说一个人使用你的库。他们可能会有如下代码:

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

//其余代码
} catch(FileNotFoundException e){
System.err.println(无法找到输入文件:+ filename);
System.err.println(Terminating ...);
System.exit(3);
}
}
}

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


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.

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.

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) { }
}

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.

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

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