我们什么时候应该在方法中抛出异常或捕获异常? [英] When should we throw exceptions, or catch exceptions, in a method?

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

问题描述

我一直在阅读有关异常的更多信息,但是我不确定在哪种情况下我们应该抛出一个方法

I've been reading up more on exceptions, but I am not sure with what cases we should either throw a method

public void fxml() throws IOException
{
     // method body here
}

或在方法中捕获异常

public void fxml()
{
          FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml.fxml"));

            try
            {
                fxmlLoader.load();
            } 
            catch (IOException exception) 
            {
                throw new RuntimeException(exception);
            } 
}

从Oracle的示例中可以看到

From Oracle's example it says


有时,代码捕获其中可能发生的异常是适当的。但是,在其他情况下,最好让调用堆栈更远的方法来处理异常。例如,如果您将ListOfNumbers类作为类包的一部分提供,则可能无法预期包中所有用户的需求。在这种情况下,最好不要捕获异常,并允许调用堆栈中的方法进一步处理该异常。

Sometimes, it's appropriate for code to catch exceptions that can occur within it. In other cases, however, it's better to let a method further up the call stack handle the exception. For example, if you were providing the ListOfNumbers class as part of a package of classes, you probably couldn't anticipate the needs of all the users of your package. In this case, it's better to not catch the exception and to allow a method further up the call stack to handle it.

https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html

所以我很好奇这是否表示我们可能不需要类/方法并通过在此方法内进行try / catch如果我们不使用它,那么它就没有目的,因此我们将它扔以备后用?

So I'm curious if this is saying that we might not need the class/method and by doing a try/catch inside this method it doesn't serve a purpose if we don't use it, so we "throw it" for later use?

似乎类本身也抛出异常以便以后使用...仅仅是抛出层次结构,直到您最终可以全部使用?在上面的教程中,有几章后面的章节称为链式异常,这实际上是方法抛出的问题,供以后使用吗?

It seems that classes themselves also "throw exceptions" in order to be used later... Is it just a hierarchy of throws until you can finally use it all? In the tutorial above, a few chapters later is a chapter called "Chained Exceptions" is this essentially what's going on with the method throws for use later?

线程何时在Java方法声明中使用throws?

,但是我发现它没有完全解释我想知道的内容,但是我发现了这种兴趣

but I found it didn't fully explain what I wanted to know, however I found this interest


如果您正在捕获异常类型,则无需抛出该异常,除非您将其重新抛出。在您发布的示例中,开发人员应该做一个或另一个,而不是两个都做。

If you are catching an exception type, you do not need to throw it, unless you are going to rethrow it. In the example you post, the developer should have done one or another, not both.

通常,如果您不打算做任何例外处理,则不应

Typically, if you are not going to do anything with the exception, you should not catch it.

您可以做的最危险的事情是捕获异常,而不对其执行任何操作。

The most dangerous thing you can do is catch an exception and not do anything with it.

我真的不确定他扔掉它的意思,除非他说要扔掉该方法并稍后再使用它?

I'm not really sure what he meant by "rethrowing" it unless he's speaking about throwing the method and catching it later?

然后他谈到如果您不打算使用它,则不做任何事情,因此,如果我们需要,最好将它扔给以后使用?

Then he talks about not doing anything with an exception if you're not going to use it, so it seems throwing it for later use is preferred in case we need it?

然后说这很危险吗?为什么会这样?

Then talking about it being dangerous? Why is this?

因此,基本上,如果我们不知道是否要使用它,则应该将其抛出以便可以调用方法本身,或者我们知道无论如何它都会被调用,那么我们应该做一个try / catch块吗?

So essentially if we don't know if we are going to use it, then we should throw it so the method itself can be called, or if we know that it's going to be called, no matter what, then we should do a try/catch block?

我还注意到我的示例还基于抛出了RuntimeException。 IOException。因此,从某种意义上讲,您可以采用我们抛出的第一个方法,无论发生什么异常,然后将其放入try或catch块中?似乎catch块更适合 RuntimeException或其他系统异常之一,但也许还有其他用例?

I also notice that my example also throws a RuntimeException based on the IOException. So in a sense you could take the first method we threw with whatever exceptions, and then throw it in either the try OR catch block? It seems the catch block is more suited for "RuntimeException" or one of those other system exceptions, but maybe there are other usecases?

思路?

感谢您的帮助!

推荐答案

如果代码无法执行,则会引发异常它的工作(也称为履行合同)。发生这种情况的原因是,呼叫者向您传递了无效的输入,或者某些外部资源出现故障(例如丢失的网络连接)。

You throw an exception if your code can't do its job (also known as "fulfilling its contract"). This might happen because the caller passed you invalid input, or some external resource is malfunctioning (such as a lost network connection).

在下游出现预期的问题时可以捕获异常。例如,您可能会捕获到指示网络问题的异常并重试该操作几次,或者您可能向用户显示错误消息并询问下一步该怎么做。

Catch an exception when there's an anticipated problem downstream that you can handle somehow. For example, you might catch exceptions indicating network problems and retry the operation a couple of times, or you might display an error message to the user and ask what to do next.

如果下游代码可能会引发异常,但是您的代码位于中间位置,并且不知道该怎么办,只需让异常传播到调用代码即可。

If downstream code might throw an exception but your code is somewhere in the middle and doesn't know what to do, just let the exception travel up to the calling code.

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

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