When to use throws in a Java method declaration?

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

    You only need to include a throws clause on a method if the method throws a checked exception. If the method throws a runtime exception then there is no need to do so.

    See here for some background on checked vs unchecked exceptions: http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html

    If the method catches the exception and deals with it internally (as in your second example) then there is no need to include a throws clause.

提交回复
热议问题