Why is it legal to re-throw a Throwable in certain cases, without declaring it?

后端 未结 3 1041
长发绾君心
长发绾君心 2020-12-09 03:08

I would expect the following code to raise a compile-time error on throw t;, because main is not declared to throw Throwable, but it c

3条回答
  •  Happy的楠姐
    2020-12-09 03:38

    Because the compiler is smart enough to know that a checked exception can not be thrown from the try block, and the caught Throwable is thus not a checked exception that must be declared.

    Note that this is true since Java 7, if I'm not mistaken.

提交回复
热议问题