Talk: You're Doing Exceptions Wrong
- Jan 11, 2024: CodeMash
Abstract
Exceptions are powerful and valuable, but we frequently misuse them and destabilize our applications. We fear users seeing an error message, so we swallow the exception, creating troubleshooting nightmares down the road. We fail to check inputs and throw them at all, letting garbage into our databases that ruins our application’s correctness. When we do actually get around to throwing an error, we use unclear messages that mislead and distract or we go overboard and cover our entire codebase with annoying try-catch blocks that make the code impossible to read.
It doesn’t have to be this way! This talk will tell you about numerous exception-related antipatterns and how to fix them. You’ll get practical examples born of real-world codebases that show you how to design your systems in ways that are easy to troubleshoot. If you’re new to object-oriented programming with exceptions, you’ll get a lot out of this talk: you’ll learn how to do exceptions the right way and enjoy safer and more maintainable code.
Main Takeaways
Most apps probably have too many catch
blocks. Unless you actually have a way to
handle them, its better to let exceptions bubble up to the top and be logged. In
other words, don’t swallow exceptions and hide the errors from yourself.
Learn to see exceptions as a gift that prevents a bug in your code from percolating further into the system and causing worse damage.
Protect your system from future programmer error by validating preconditions.
Outline
- Exceptions get a bad rap
- Category 1: Fatal Exceptions
- Category 2: Boneheaded Exceptions
- Category 3: Vexing Exceptions
- Category 4: Exogenous Exceptions
- Principles
Resources
- Slides (19MB pdf)
- Vexing Exceptions by Eric Lippert