Section 1.12 Runtime Errors
The second type of error is a runtime error, so called because the error does not appear until you run the program. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.
Runtime errors are rare in the simple programs you will see in the first few chapters, so it might be a while before you encounter one.
Using English as an example, think of this statement: “Put an open bottle of ink in your shirt pocket.” The syntax (structure) of the statement is fine, but if you follow the command, something bad will happen to your shirt pocket.
Here’s another example. Consider these instructions:
You have five boxes of shirts, numbered 1 to 5, next to a table.
Start with box 1.
Take out a shirt from the box and put it on the table.
Go to the next numbered box.
Repeat steps c and d.
Again, each step is gramatically correct, but you’ll have a problem if you try to carry out the instructions. Since the instructions don’t tell you when to stop, after you take the shirt out of box five, there’s no “next numbered box” to go to!
Here’s another example. Consider these instructions:
Ask a friend for a number (let’s call it a).
Ask a friend for another number (let’s call it b).
Divide a by b and tell your friend the result.
The syntax (grammar) of each step is fine. When you carry out the instructions, if your friend gives you zero for their second number, you won’t be able to do step three, because you can’t divide by zero. The error doesn’t show up until you carry out the instructions.
Here’s the Python code for this example. Try running it and entering zero for the second number, and you’ll see the error occur while you are running the program.
In all these examples, you can read and understand all the instructions; their “grammar” is fine. The run-time error happens when you try to actually do the instructions; that is when you are running the program.
Check your understanding
Checkpoint 1.12.1.
Which of the following is a run-time error?
Attempting to divide by 0.
- Python cannot reliably tell if you are trying to divide by 0 until it is executing your program (e.g., you might be asking the user for a value and then dividing by that value - you cannot know what value the user will enter before you run the program).
Forgetting a colon at the end of a statement where one is required.
- This is a problem with the formal structure of the program. Python knows where colons are required and can detect when one is missing simply by looking at the code without running it.
Forgetting to divide by 100 when printing a percentage amount.
- This will produce the wrong answer, but Python will not consider it an error at all. The programmer is the one who understands that the answer produced is wrong.
Checkpoint 1.12.2.
Who or what typically finds runtime errors?
The programmer.
- Programmers rarely find all the runtime errors, there is a computer program that will do it for us.
The interpreter.
- If an instruction is illegal to perform at that point in the execution, the interpreter will stop with a message describing the exception.
The computer.
- Well, sort of. But it is a special thing in the computer that does it. The stand alone computer without this additional piece can not do it.
The teacher / instructor.
- Your teacher and instructor may be able to find most of your runtime errors, but only because they have experience looking at code and possibly writing code. With experience runtime errors are easier to find. But we also have an automated way of finding these types of errors.