[jboss-dev] throws Throwable

David M. Lloyd david.lloyd at redhat.com
Wed May 6 14:21:33 EDT 2009


On 05/06/2009 11:43 AM, Tim Fox wrote:
> David M. Lloyd wrote:
>> On 05/05/2009 03:02 AM, Carlo de Wolf wrote:
>>> What's the use case for having a public method: doSomething(Object 
>>> someArgs...) throws Throwable ?
>>
>> No valid use cases exist afaik.  Any method that throws Throwable 
>> should die, if it's within our power to kill it.
> Declaring a method as "throws Throwable" forces the caller to handle the 
> exception even if the exception/error thrown is unchecked.
> 
> E.g.
> 
> void storeMessage(Message message) throws Throwable
> {
> }
> 
> If that method fails in any way, even if it throw a RuntimeException or 
> Error then we need to handle it, since it would be a major problem if 
> the message didn't get saved.
> 
> The method could be declared to not throw Throwable and the caller would 
> just have to remember to catch (Throwable) but declaring the method as 
> throws Throwable prevents the possibility of the caller from forgetting 
> to do so.

That's a terrible idea.  Please - never do this. :-)

The furthest one should go towards "reminding" the user to handle a runtime 
exception is to add that exception to the "throws" clause of the method, 
and document it.

- DML




More information about the jboss-development mailing list