Exceptions were designed for “exceptional conditions,” when you simply don’t know what to do within your current context. C++ steered us toward using exceptions for error reporting and Java institutionalized it. And yet, sometimes it seems akin to pulling a fire alarm when the floor gets dirty, the sink clogs or the dog has an accident.
What should we do instead? Certainly, some conditions justify exceptions, but numerous others are expected errors; Forcing the caller to use try and except right at the call site seems overkill. C showed us that returning special values never worked out very well—we could never settle on the values and the resulting interface didn’t make it clear enough to the caller that they should respond to error conditions.
The standard error-reporting practice in the Go language is to return a tuple holding success and null-or-failure objects, while in Scala you return a disjoint union that can be either a success object or a failure object. Both cases require the programmer to analyze the result; you can no longer just assume the result is valid. This produces the same effect as checked exceptions (that is, you can’t ignore the fact that the expression might fail) but without the visual noise and distraction of exception handling.
Based on the Scala and Go approaches, I’ll show how you can modify your Python coding style to produce more robust error handling.
Bruce Eckel is the author of Thinking in Java and Thinking in C++, and a number of other books on computer programming. He’s been in the computer industry for 30 years and tends to reach for Python when he needs to solve real problems. He’s given hundreds of presentations around the world and enjoys putting on alternative conferences and events like The Java Posse Roundup. He is currently studying organizational dynamics, trying to find a new way to organize companies so that working together becomes a joy; you can read about his struggles in this arena at Reinventing-Business.com, while his programming work can be found through www.MindViewInc.com.
Help us make this conference the best it can be for you. Have questions you'd like this speaker to address? Suggestions for issues that deserve extra attention? Feedback that you'd like to share with the speaker and other attendees?
Join the conversation here (requires login)
For information on exhibition and sponsorship opportunities at the conference, contact Sharon Cordesse at (707) 827-7065 or firstname.lastname@example.org.
View a complete list of OSCON contacts