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?) and dictionaries (does the key exist?). Ask yourself if you should ignore the problem, handle it by checking values first, or handle it with an exception. Pay special attention to areas where you might have used finally
and else
to ensure the correct...