When to use throws in a Java method declaration?

后端 未结 7 1216
情话喂你
情话喂你 2020-12-07 14:10

So I thought I had a good basic understanding of exception-handling in Java, but I was recently reading some code that gave me some confusion and doubts. My main doubt that

7条回答
  •  一整个雨季
    2020-12-07 14:40

    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.

    A good discussion of when it is appropriate to throw exceptions is here

    When to throw an exception?

提交回复
热议问题