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?
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
ExceptionsandErrors. SinceErrorsby definition arise from unsalvagable conditions, it is pointless to include them in your method declaration. That leaves justException.You should declare your method with
throws Exceptioninstead.Note that the narrower the range of
throwsthe better.Declaring your method to be
throws Exceptionis ok if your method doesn’t generate the exceptions, but instead calls other code that is declared asthrows Exceptionand 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