Uses
Posted Mar 31, 2012 22:52 UTC (Sat) by
cmccabe (guest, #60281)
In reply to:
Uses by Cyberax
Parent article:
Go version 1 released
I am going to use some Java terminology here, so bear with me. "Unchecked" exceptions are those that are not required to be caught by anyone. In other words, they are not enforced by the type system. "Checked" exceptions, on the other hand, must be either declared to be thrown by the caller, or handled by the caller.
Unchecked exceptions remove information from the type system, which makes the system less robust, for the same reason using void* and not enforcing invariants does. C++, Python, and many other languages ONLY have unchecked exceptions. In many cases, it's difficult even to get information about what exceptions can be thrown, let alone what to do about them, without reading the entire source code of the function you're calling and all the functions it calls. I've been in this situation before, and it's not pretty. For me at least, this kind of "error handling" is really "error ignoring" since the likelihood that maintenance programmers will know all that stuff approaches zero as time goes on.
Checked exceptions, on the other hand, tend to be just as verbose (and sometimes more verbose) than simple if-then error handling. For example:
try {
if (is_set(ctx, 'debug_mode') && is_set(ctx,'show_log'))
do_something();
} catch (IOException e) {
throw new BlahFailedException("blah failed! Error: " +
e.getMessage());
} catch (DoSomethingException e) {
throw new BlahFailedException("do_something failed! Error: " +
e.getMessage());
}
I'm giving you a best-case scenario here-- I didn't have to add a finally block or do something special with RuntimeException, which I often do.
And if you think it's always possible to change your function signature to declare all the checked exceptions you want to throw, think again. For example, if you are implementing an interface like SAXParser or something, you don't get that kind of choice. You'll have to catch and translate every checked exception into either a RuntimeException (which ruins type-safety) or a SAXParseException (which is misleading and verbose to implement).
A final comment. If you design the system such that there are a lot of corner cases are tricky situations, then yes, your code will be tricky. On the other hand, if you design the system such that it's simple, you won't feel the need for complexity. For example, you could load the entire configuration at once and keep it in memory, eliminating the need to handle the "file not found" error condition that you're so worried about. Exceptions don't reduce the complexity of code. At best, they just sweep it under the rug. And that's not a good thing, for anyone concerned.
Another note. In its newest language, Ceylon, Red Hat is getting rid of checked exceptions. Google coding standards for C++ dictate no use of exceptions-- ever. I think the way Go handles exceptions is the right way.
(
Log in to post comments)