When to use throws in a Java method declaration?

后端 未结 7 1228
情话喂你
情话喂你 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:50

    The code that you looked at is not ideal. You should either:

    1. Catch the exception and handle it; in which case the throws is unnecesary.

    2. Remove the try/catch; in which case the Exception will be handled by a calling method.

    3. Catch the exception, possibly perform some action and then rethrow the exception (not just the message)

提交回复
热议问题