Non-Programmer's Tutorial for Python 2.6/Dealing with the imperfect

...or how to handle errors
So you now have the perfect program, it runs flawlessly, except for one detail, it will crash on invalid user input. Have no fear, for Python has a special control structure for you. It's called  and it tries to do something. Here is an example of a program with a problem:

Notice how when you enter  it outputs something like:

Traceback (innermost last): File "try_less.py", line 4, in ? number = int(raw_input("Enter a number: ")) ValueError: invalid literal for int: @#&

As you can see the  function is unhappy with the number   (as well it should be). The last line shows what the problem is; Python found a. How can our program deal with this? What we do is first: put the place where the errors occurs in a  block, and second: tell Python how we want  s handled. The following program does this:

Now when we run the new program and give it  it tells us "That was not a number." and continues with what it was doing before.

When your program keeps having some error that you know how to handle, put code in a  block, and put the way to handle the error in the   block.

Here is a more complex example of Error Handling.

The program above uses concepts from previous lessons as well as the current lesson. Let's look at the above program in sections.

After we define the function called "main", we tell it that we want to "try" function named "epact". It does so "while" there is no "success". The interpreter then goes to the line. The interpreter takes the value entered by the user and stores it in the variable named "year".

If the value entered is not an integer or a floating point number (which would be converted to an integer by the interpreter), an exception would be raised, and execution of the  block ends, just before   is assigned the value 1.

Let's look at some possible exceptions. the program above does not have an  clause for every possible exception, as there are numerous types or exceptions.

If the value entered for year is an alphabetical character, a  exception is raised. In the program above, this is caught by the  line, and the interpreter executes the print statement below the , then it sets the value of "year" to 0 as a precaution, clearing it of any non-numeric number. The interpreter then jumps back to the first line of the  loop, and the process restarts.

The process above would be the same for the other exceptions we have. If an exception is raised, and there is an except clause for it in our program, the interpreter will jump to the statements under the appropriate except clause, and execute them.

The  statement, is sometimes used in exception handling as well. Think of it as the trump card. Statements underneath the  clause will be executed regardless of if we raise and exception or not. The  statement will be executed after any   or   clauses prior to it.

Below is a simpler example where we are not looped, and the  clause is executed regardless of exceptions.

If we were to enter an alphabetic value for, the output would be as follows:

Please enter a number. t Error. I am executing the finally clause.

Exercises
Update at least the phone numbers program (in section ../File IO/) so it doesn't crash if a user doesn't enter any data at the menu.