java什么时候主动抛异常,我们什么时候应该在方法中抛出异常或捕获异常?

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

}

or catch an exception within a method

public void fxml()

{

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

try

{

fxmlLoader.load();

}

catch (IOException exception)

{

throw new RuntimeException(exception);

}

}

From Oracle's example it says

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.

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?

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?

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?

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?

Thoughts?

Thanks for the help!

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值