4 ms·
The way I see it exceptions are helpful in creating self documenting code if used thoughtfully and effectively.* Some might argue subclassing the base exceptio
by FuzzyDunlop 15y ago
The way I see it exceptions are helpful in creating self documenting code if used thoughtfully and effectively.*
Some might argue subclassing the base exception to create more specifically named ones is a bit silly, because you may be doing little more than renaming a class several, maybe many, times. But it may be countered that this is simpler than determining a list of error codes and then leaving it to other people to find out what a random string or integer even means. I personally haven't found this helpful for debugging.
Both ways are better than something just returning false and leaving you to figure out why it did that in the first place.
The main benefit to me, however, is to be able to throw the exceptions deeper in the code (where appropriate) and to be able to then catch them at the very last moment. While not perfect (I can't account for everything), it allows me to keep my error handling code cleanly separated from the rest of it at the most abstracted point. Anything lower level will by necessity be a little messier.
eg. not wrapping an entire* script in one try/catch block.