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

前端 未结 6 1181
失恋的感觉
失恋的感觉 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条回答
  •  猫巷女王i
    2020-11-27 07:27

    java.lang.Exception is a checked exception so this won't work or even compile. It would work with a unckeched (java.lang.RuntimeException). It makes absolutly no difference whether you throw an exception inside a catch block or not.

    The compiler error would look something like this (depending on the compiler):

    java: unreported exception java.lang.Exception; must be caught or declared to be thrown

    EDIT: Java 7 can deal with such situations if you never actually throw the exception

提交回复
热议问题