Is "throws Throwable" good practice

java

Solution

You should not throw `Throwable`. Here's why.

Throwable is the top of the hierarchy of things that can be thrown and is made up of `Exceptions` and `Errors`. Since `Errors` by definition arise from unsalvagable conditions, it is pointless to include them in your method declaration. That leaves just `Exception`.

You should declare your method with `throws Exception` instead.

Note that the narrower the range of `throws` the better.

Declaring your method to be `throws Exception` is ok if your method doesn't generate the exceptions, but instead calls other code that is declared as `throws Exception` and you want exceptions to percolate up the call stack.

If your method is the generating the exception, then declare a narrower range, eg `throws IOException, MyProcessingException`, etc

Problem

In the past I'd read tons of code with methods like: ``` public Object doSomething() throws Throwable { ... } ``` Is it common practice to do that? What are pros & cons? `throws Trowable` seemed to me like the "Agent Orange" way of getting the Exception- matter done EDIT Handle expected Exceptions in the Method Throw unexpected Exceptions (one by one) Don't care of Errors Is that the way to go?

Original source

Related problems