Conventions for exceptions or error codes [closed]

In high-level stuff, exceptions; in low-level stuff, error codes.

The default behaviour of an exception is to unwind the stack and stop the program, if I’m writing a script an and I go for a key that’s not in a dictionary it’s probably an error, and I want the program to halt and let me know all about that.

If, however, I’m writing a piece of code which I must know the behaviour of in every possible situation, then I want error codes. Otherwise I have to know every exception that can be thrown by every line in my function to know what it will do (Read The Exception That Grounded an Airline to get an idea of how tricky this is). It’s tedious and hard to write code that reacts appropriately to every situation (including the unhappy ones), but that’s because writing error-free code is tedious and hard, not because you’re passing error codes.

Both Raymond Chen and Joel have made some eloquent arguments against using exceptions for everything.

Leave a Comment