Error Handling Examples

Rizwan Qaiser
3 min readSep 16, 2018

--

work in progress

One of the most frustrating aspect of programming is when you’re in the flow of writing your code but run into an error. Often these errors are non-descriptive and are difficult to understand, let alone handle.

Covering edge cases and handling errors helps you create robust, technically sound systems, which help you to delight the user (including yourself).

Good error handling can be especially powerful if your program if your program is small-enough not to avoid multiple test cases.

In the following article, we will provide an overview on how you can design robust systems with effective error handling using Python 3.6.3. This article is sourced from my personal experience as well as the official documentation with edits and changes in places where I felt it was lacking or could be improved for the reader.

Handling Errors with Try Except

The Try/Except syntax is one of my favourite parts of Python. Not only is it effective, it makes intuitive sense. Essentially, you’re telling your code to try and do something an if there’s an error or an exception, handle it a certain way.

It is possible to write programs that handle selected exceptions. Look at the following example, which asks the user for input until a valid integer has been entered.

>>> while True:
... try:
... x = int(input("Please enter a number: "))
... break
... except ValueError:
... print("Oops! That was no valid number. Try again...")
...

The try statement works as follows.

  • First, the try clause (the statement(s) between the try and except keywords) is executed.
  • If no exception occurs, the except clause is skipped and execution of the try statement is finished.
  • If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.
  • If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.

A try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try statement. An except clause may name multiple exceptions as a parenthesized tuple, for example:

... except (RuntimeError, TypeError, NameError):
... pass
  • Multiple Excepts
  • Raise for Custom Error Message
  • Try Except Else

Folly of Try/Except

Sometimes a programmer can get into the habit of just using Try/Except for all their edge cases. While there isn’t anything bad doing this in practice, but too many Try/Excepts can be a sign of poorly written code. Rather than handling all your errors in the various edge cases, you should take it as a sign to refactor your code.

If you’re expecting many errors, this means that your code is…well buggy. You have to remember what you’re trying to do. We are creating systems here. And good systems can get complex quickly.

So we need to make sure that this complexity isn’t handled as an afterthought using patchwork solutions. Complexity should be handled in the logic of your code.

It’s easy to fall into using Try Excepts and forget that you can write code that better handles the corner cases that you’re trying to avoid.

One must be aware that it is simply one way to solve a problem. Some times your code needs to be streamlined enough that it only solves your one specific problem. Other times, it may need to be robust. One must always keep in mind the user. Wether it is oneself, a client, or someone in the future who will be an administrator or the system.

A true designer of systems needs to be aware that systems are holistic, and that they are complex. This complexity is beauty.

--

--