Exercises

If you've never dealt with exceptions before, the first thing you need to do is look at old Python code you've written and notice if there are places you should have been handling exceptions. How would you handle them? Do you need to handle them at all? Sometimes letting the exception propagate to the console is the best thing to do. Sometimes you can recover from the error and allow the program to continue. Sometimes you can only reformat the error into something the user can understand and display it to them.

Some common places to look are file I/O (is it possible your code will try to read a file that doesn't exist?), mathematical expressions (is it possible that a value you are dividing by is zero?) list indices (is the list empty?) ...

Get Python 3 Object Oriented Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.