The base exception is just Exception, but there are specific ones after that. From the previous slides, and our past experience programming, some exceptions we've seen include:
ZeroDivisionError
IndexError
TypeError
NameError
ValueError
What do all of These Errors Mean!?
ZeroDivisionError
IndexError
TypeError
NameError
ValueError
ZeroDivisionError - divide by zero
IndexError - index out of range
TypeError - function or operation applied to inappropriate type
NameError - name (variable, function, etc) doesn't exist / not yet declared
ValueError - function or operation gets right type, but inappropriate value
So Many Exceptions
The root Exception, and the other exception types that follow from it is called an exception hierarchy
Great, so Now What?
We can gracefully recover from exceptions!
For example, a common source of runtime errors is user input…
A Short Program
Let's write a simple interactive program that converts inches to feet:
ask for numeric input - some number of inches
accept decimal values as well, such as 1.5 inches
divide that number by 12 to get feet
print out the result
Soooo… That Works Great
Let's try running the program…
Everything works fine until? Is there a certain kind of input that will cause an error in this program?
Can We Prevent This Error from Happening?
…Maybe! Let's try a couple of things. →
How about isdigit and isnumeric?
But they don't let legitimate input through!
"Defensive Programming" Continued
Any other ways to allow strings like 3.2 in, but still prevent strings that are not composed numbers and a decimal point? →
loop through every character, and make sure that it's only a number or a decimal point!?
use find to check for decimal; create another string without that and check if it's a digit
ughhhh … nevermind, these all seem a bit clunky
EAFP
Sometimes it's…
Easier to Ask Forgiveness than Permission
instead of defensive programming…
let's just try it
and ask for forgiveness if we made a mistake
Exception Handling
There's a construct in most programming languages that lets you handle exceptions. In python, that construct is a try-except block. It's similar to an if-else:
try-except
The try block watches out for any statements within that block that causes errors.
If there is an error, the code in the except block will be run.
Otherwise, the code will execute normally (ignoring the except block)
try-except Example 1
What is the output of this code?
try-except Example 2
What is the output of this code?
Let's Take Another Look at Our Conversion Program
Let's modify our program so that it behaves in a similar way, but uses try-except instead of testing with an if statement first.
Other Exceptions
We saw that we could handle a ValueError in our program. Can that exception happen in the following program? Are there any other exceptions (that we just talked about in an earlier slide) that can happen? →
ValueError - for non numeric input
ZeroDivisionError - for 0 as input
Fixing the Pizza Pie Problems
How do we fix it (original code below)? →
Fixing the Pizza Pie Problems Continued
What if we want to deal with ValueErrors and ZeroDivisionError differently?
Say either:
That's not a number! (ValueError)
More for me! (ZeroDivisionError)
Specific Exceptions, Multiple Except Blocks
You can catch specific exception types, and add an arbitrary number of except blocks for every exception type that may occur.