I strongly recommend you don't solve this by throwing a RuntimeException
.
Those are strictly the preserve of the JVM and it would be dreadful style to use one just to avoid declaring a throw clause.
The name and purpose of the Java exceptions is (IMHO) confused.
You should stick to the idea that things subclassed from Exception
need handlers and things derived from Error
don't. Ignore the rule-breaker RuntimeException
and complain bitterly to your mates down the pub and random people on stackoverflow.com that it ought to just be called RuntimeError
and sub-class Error
(for f**ks sake).
If you really have a condition where a method has a throws clause declaring an Exception
but you have good reason to now throw it up then I suggest you sub-class Error
to either UnexpectedExceptionError
or UnhandledExceptionError
.
Use the first if the Exception
is 'not supposed to be possible - such as an abstract I/O method that you know is backed by (say) a string in memory and can't really throw IOException
despite the interface declaration.
Use the second if throwing the Exception
would break the signature of some method you're implementing/overriding that can't be modified to advertise the potential Exception
.
As a third-way it's natural for exceptions to propagate to the run()
method of Runnable
.
In those cases I recommend passing the Exception
to some overseer process as a kind of suicide note because whatever you throw out of run()
will get ignored anyway and you probably want do something to recognize complete failure of a Thread
!
PS: If you're doing this because you're lazy and don't want to handle exceptions properly, may you be cursed to maintain code written by someone with the same attitude :)