Rethrowing an Exception: Why does the method compile without a throws clause?

前端 未结 6 1156
失恋的感觉
失恋的感觉 2020-11-27 06:40

On the source code below I\'m rethrowing an Exception.
Why is it not necessary to put the throws keyword on the method\'s signature?

         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 07:16

    When you use throws with a method, it means that the statement which will call that method must be surrounded with a try catch block.

    But if the method already includes try catch block then no thorws declaration is needed as the exception being thrown by the method is being handled there only.

    The statement calling this method does not need a to be surrounded with try catch block.

    Hope this clears your doubt.

提交回复
热议问题