10 Exceptions and resources
This chapter covers
- Downsides to exception usage
- Benefits of good exception usage
- The relationship between resource handling and exceptions
- Building an exception hierarchy for precise control
The topic of exceptions has proponents and detractors across the entire spectrum, with developers advocating from using them rarely, if ever, to using them frequently. Over the years, several authors have published articles arguing for various positions along this spectrum. Many of these arguments are relevant, yet they mesh poorly with other viewpoints. This situation leaves us in a quandary when approaching exception usage. Many positions are correct within specific problem domains but are problematic in others. There are numerous situations in which one policy is meaningful but would make little sense in others.
Exceptions need to be considered in the scope of the entire program. A policy that addresses the behavior of a function may not be meaningfully extended to larger units. Many current programs running in production need to have the luxury of redesigning to handle a unified strategy for exceptions. In these myriad cases, almost any design is better than none. Yet integrating localized strategies into larger units may prove frustrating and error-prone.