Errors that end your program early#
It’s inevitable: things go wrong in your program, causing it to terminate early. There are a number of ways this can happen:
Syntax errors mean you can’t even run your program, like
def do_nothing(x) return x
.Reading from an undefined variable might crash your program when it runs, like
def f(x): return x + n
.Misusing values might lead to a type error, like
1 + 'hi'
.Some operations are partial, like division or
list.index
.Someone could hit
^C
(i.e., control-c) and interrupt your program.Someone could turn off the computer, e.g., the battery dies.
Each of these circumstances is some kind of error. Programmers spend a great deal of time anticipating, avoiding, and handling errors. Let’s look at each kind of error in turn.
A bit of nomenclature: in general computing, error and exception are more or less synonymous. But in Python, an error is a particular kind of exception. For now, we can treat the terms as synonymous.