In further anecdotal evidence that Gilad was right, Neal Gafter has blogged with a suggestion that checked exceptions could be removed from Java. He's right, they could. As far as I can see, all existing code would still compile, and what's more, it would actually work with existing bytecode with no problem, as the runtime has no problem with throwing undeclared checked exceptions. I've thought for a while that the problem with checked exceptions is that the API that throws them doesn't know whether you will care to deal with them.
First, I'll mention some ways in which closures can help with checked exceptions, then a mechanism for optional checked exceptions:
Reader.close() can throw an IOException, but we don't let it propagate. Closures to the rescue:
suppressing(IOException.class){ reader.close();}
Making Swing use the native look and feel requires catching about 5 exceptions, but we don't care about any of them:
loggingAndIgnoring(Exception.class){ UIManager.setLookAndFeel..}
Or even better:
whenExceptionsHappen({e => e.printStackTrace();}){ UIManager.setLookAndFeel...}
Suppose that we do want to propagate the exception instead, but we don't want to wrap it in a RuntimeException:
public void readsFromAFile() spits IOException
I'm not seriously suggesting 'spits' as a keyword, feel free to think of your own (I also like 'farts' and 'vomits', but that's because I've had a very busy weekend), but in this context, a method that spits IOException is declaring that it can throw an IOException, but that callers of it do not have to catch the IOException. That way everyone can be reasonably happy. API designers can still declare their exceptions using throws, guaranteeing that the client programmer has to think about the exception, but the client programmer can prevent the exception from leaking through their codebase, by using spits.
There is one problem - catching a checked exception from code that does not throw that exception is currently a compile-time error. Spat exceptions would make that compile-time error need to disappear too.
Challenge: think of a better keyword than 'spits' for this.