Chapter 7 in Python 101.

Exceptions are runtime errors in python. Something went wrong in a very specific way (e.g. you tried to divide by zero). A full list of errors can be seen in the python documentation, but a useful shortlist from the Python 101 is as follows:

  • Exception (this is what almost all the others are built off of)
  • AttributeError - Raised when an attribute reference or assignment fails.
  • IOError - Raised when an I/O operation (such as a print statement, the built-in open() function or a method of a file object) fails for an I/O-related reason, e.g., “file not found” or “disk full”.
  • ImportError - Raised when an import statement fails to find the module definition or when a from … import fails to find a name that is to be imported.
  • IndexError - Raised when a sequence subscript is out of range.
  • KeyError - Raised when a mapping (dictionary) key is not found in the set of existing keys.
  • KeyboardInterrupt - Raised when the user hits the interrupt key (normally Control-C or Delete).
  • NameError - Raised when a local or global name is not found.
  • OSError - Raised when a function returns a system-related error.
  • SyntaxError - Raised when the parser encounters a syntax error.
  • TypeError - Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.
  • ValueError - Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.
  • ZeroDivisionError - Raised when the second argument of a division or modulo operation is zero.

The interesting and useful bit is that you can code predefined actions on the basis of specific errors using a try/except structure that functions somewhat like an if/else statement. try: essentially says "run this specific code" and except [exception type]: says that if that exception type occurs, run this other code. Let's look at an example below:

    #!/usr/bin/python3

    try:
        999 / 0
    except ZeroDivisionError:
        print("Dividing by Zero is an Error and a Paradox")

The concept of a bare excerpt is when you don't define the type of error you expect like ZeroDivisionError above, and instead just have except: on its own. The bare excerpt will catch any exception, so it may be less useful for debugging.

Thankfully―and again just like an if statement―you can have several except statements just as you would have several elif statements in order to parse different types of exceptions:

try:
	[something]
except ZeroDivisionError:
	[something]
except IndexError:
	[something]
except KeyError:
	[something]

There's also an else and a finally statement. Else will run only if there are no errors. Finally that one can always run at the end of a try/except block to take care of any necessary business before exiting the program, even when an error has occurred (e.g. closing a file).

try:
	[something]
except ZeroDivisionError:
	[something]
else:
	[run only if there are no errors]
finally:
	[do this]

General Thoughts

It was not clear to be before reading this chapter that I had the power to specifically find and flag types of errors to debug my programs. This proves useful in two ways:

  1. I understand how to specifically flag if a certain error is occurring and
  2. I understand what errors can actually occur (it's nice to have a list)!