Is “throws Throwable” good practice

前端 未结 9 1030
攒了一身酷
攒了一身酷 2020-12-30 21:14

In the past I\'d read tons of code with methods like:

public Object doSomething() throws Throwable {
    ...
}

Is it common practice to do

9条回答
  •  一整个雨季
    2020-12-30 21:37

    Functionally, it is equivalent with throws Exception, since errors are unchecked.

    I see no reason to declare a method to throw Throwable. However, this doesn't mean that catch and printStackTrace is a good alternative.

    Usually, you want to catch throwables where you can do something sensible with them.

    Code that throws a throwable you don't expect should explode gloriously, so you can see the error and fix the bug.

提交回复
热议问题