Is “throws Throwable” good practice

前端 未结 9 1105
攒了一身酷
攒了一身酷 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:39

    Is it common practice to do that?

    In the JDK it is rare. This is mostly used when it is not clear how to handle checked exceptions.

    What are pros & cons?

    The pros is that you get your code to compile without worrying about checked exception.s

    The cons is that exception you should be handling are being ignored.

    Isn't it better to catch and printStackTrace()?

    Unhandled exception are usually printed anyway so catching them doesn't help much.

    You should catch an exception when you can add some value by doing so and add the exception to the throws clause when you can't.

提交回复
热议问题