In an effort to avoid potentially unexpected exceptions from a method (in the interest of program stability) I recently found myself using mixed return values — bool(true) for successful execution and a string describing the error on failure. The class then acts accordingly based on the return value.
I feel like the code might be easier to understand for a reader if I just threw the exception and caught it appropriately in the case of an error.
I have a nagging feeling that the TRUE/string mixed return is somehow ugly — at least I don’t see it often and I can’t say I’ve used it much before. At the same time the method is not so critical that if it failed and an exception wasn’t caught it would be worth breaking the whole app. Am I totally OCD/paranoid here? Does it even matter? HELP ME STOP THE INSANITY 🙂
Exceptions are for exceptions (unexpteced and/or severe failure of a program part). They hardly are an easy to use replacement for handling errors you can anticipate. My $0.02. 😉
So if your method is critical and encounters a critical failure, throwing an exception might be the right way to go about it. Defining meaningful error values/descriptions in global or class scope and returning these might be another valid way to handle errors there.