Exception Handling
Right now, getting an error, or exception, in your Python program means the entire program will crash. You don’t want this to happen in real-world programs. Instead, you want the program to detect errors, handle them, and then continue to run.
For example, consider the following program, which has a divide-by-zero error.
def spam(divideBy):
return 42 / divideBy
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
We’ve defined a function called spam
, given it a parameter, and then printed the value of that function with various parameters to see what happens. This is the output you get when you run the previous code:
21.0
3.5
Traceback (most recent call last):
File "C:/zeroDivide.py", line 6, in <module>
print(spam(0))
File "C:/zeroDivide.py", line 2, in spam
return 42 / divideBy
ZeroDivisionError: division by zero
A ZeroDivisionError
happens whenever you try to divide a number by zero. From the line number given in the error message, you know that the return statement in spam()
is causing an error.
Errors can be handled with try
and except
statements. The code that could potentially have an error is put in a try
clause. The program execution moves to the start of a following except
clause if an error happens.
You can put the previous divide-by-zero code in a try
clause and have an except
clause contain code to handle what happens when this error occurs.
def spam(divideBy):
try:
return 42 / divideBy
except ZeroDivisionError:
print('Error: Invalid argument.')
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
When code in a try
clause causes an error, the program execution immediately moves to the code in the except
clause. After running that code, the execution continues as normal. The output of the previous program is as follows:
21.0
3.5
Error: Invalid argument.
None
42.0
Note that any errors that occur in function calls in a try
block will also be caught. Consider the following program, which instead has the spam()
calls in the try
block:
def spam(divideBy):
return 42 / divideBy
try:
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
except ZeroDivisionError:
print('Error: Invalid argument.')
When this program is run, the output looks like this:
21.0
3.5
Error: Invalid argument.
The reason print(spam(1))
is never executed is because once the execution jumps to the code in the except
clause, it does not return to the try
clause. Instead, it just continues moving down the program as normal.