Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: can't throw Date: Tue, 11 Sep 2012 17:02:13 -0400 Organization: A noiseless patient Spider Lines: 39 Message-ID: References: <19af6b94-7a1e-4491-afb2-79782406f560@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 11 Sep 2012 21:02:14 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="ffb8f7085759b339c1002252b48331a4"; logging-data="32195"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dn0N1E9uOh8oKE+hWbzhz" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120907 Thunderbird/15.0.1 In-Reply-To: <19af6b94-7a1e-4491-afb2-79782406f560@googlegroups.com> Cancel-Lock: sha1:CoieDO4VkY64u85xN1LEKys7/sI= Xref: csiph.com comp.lang.java.programmer:18653 On 9/11/2012 4:16 PM, bob smith wrote: > Am I the only one who wanted to throw an Exception but couldn't because I was overriding > a method that threw nothing? Happens all the time. > In particular, I'm subclassing Thread and can't throw an Exception in the run method. I suspect this is a more general issue though. The usual answer is to throw some kind of RuntimeException, with the original Exception as its "cause." For example, void method() { // no "throws" try { somethingThatCanThrow(); } catch (IOException ex) { throw new IllegalStateException( "Fertilizer on fan", ex); } } IllegalArgumentException and IllegalStateException are the wrappers I find myself using most; there are plenty of others, and of course you can implement your own RuntimeException subclass if nothing seems suitable to the situation. Some people, deeper thinkers than I, consider the whole business of checked exceptions a nuisance or a misfeature. The need for a dodge like the above can be seen as evidence for that viewpoint, but I don't find it overwhelmingly convincing. In any event, that's Java As It Is And Is Likely To Remain, so we've got to get used to it whether we sneer at it or not. Personally, I find it helpful that the compiler reminds me when I've forgotten to deal with a checked exception; the inconvenience of wrapping checked in unchecked exceptions seems a minor matter. -- Eric Sosman esosman@ieee-dot-org.invalid "The speed at which the system fails is usually not important."