RuntimeException ftw
Posted Oct 29, 2012 18:50 UTC (Mon) by
man_ls (subscriber, #15091)
In reply to:
RuntimeException ftw by cmccabe
Parent article:
Haley: We're doing an ARM64 OpenJDK port!
Sorry, I meant "Exceptions are good also to throw from APIs, as long as they are documented".
But throwing a NullPointerException is not what I said; that is obviously a bad idea in any case. Just declare an unchecked exception for every failure condition (with an umbrella super-exception) and document them. That way you can throw them from any point in your code. This is not different from your description: "document how you can fail, and come up with return codes or another API to handle it cleanly". Unchecked exceptions are a wonderful way to handle failures cleanly; in effect these exceptions become another part of your API.
IMHO, avoid checked exceptions like the plague because they become, without exception, a mess. In a recent Android app I thought I could get off with a checked exception for offline mode to force implementors to deal with it, but in the end I had to change it back to unchecked.
(
Log in to post comments)