flâneur — a map of the web's best reading

Robust exception handling - Eli Bendersky's website

eli.thegreenplace.net · 1,478 words · saved by 1 readers

Exceptions are better than returning error status codes. Some languages (like Python) leave you with no choice but to handle exceptions, as the whole language core and standard libraries throw them. But even in languages where this is not so, you should prefer exceptions to error codes. In the Python world this is sometimes called the EAFP (Easier to Ask for Forgiveness than Permission) versus LBYL (Look Before You Leap) debate. To quote Alex Martelli1 (because I couldn't have said it better myself): A common idiom in other languages, sometimes known as "look before you leap" (LBYL), is to check in advance, before attempting an operation, for all circumstances that might make the operation invalid. This approach is not ideal for several reasons: 1. The checks may diminish the readability and clarity of the common, mainstream cases where everything is okay. 2. The work needed for checking may duplicate a substantial part of the work done in the operation itself. 3. The programmer might

Executive introduction Exceptions are important This article is mainly intended for my own consumption, so take it with a grain of salt The examples are in Python, but the ideas apply to most languages Exceptions vs. error status codes Exceptions are better than returning error status codes. Some languages (like Python) leave you with no choice but to handle exceptions, as the whole language core and standard libraries throw them. But even in languages where this is not so, you should prefer exceptions to error codes. In the Python world this is sometimes called the EAFP (Easier to Ask for For

Explore this link on the map →

related reading