Program Arcade GamesWith Python And Pygame
When something goes wrong with your progam, do you want to keep the user from seeing a red Python error message? Do you want to keep your program from hanging? If so, then you need exceptions.
Exceptions are used to handle abnormal conditions that can occur during the execution of code. Exceptions are often used with file and network operations. This allows code to gracefully handle running out of disk space, network errors, or permission errors.
There are several terms and phrases used while working with exceptions. Here are the most common:
- Exception: This term could mean one of two things. First, the condition that results in abnormal program flow. Or it could be used to refer to an object that represents the data condition. Each exception has an object that holds information about it.
- Exception handling: The process of handling an exception to normal program flow.
- Catch block or exception block: Code that handles an abnormal condition is said to “catch” the exception.
- Throw or raise: When an abnormal condition to the program flow has been detected, an instance of an exception object is created. It is then “thrown” or “raised” to code that will catch it.
- Unhandled exception or Uncaught exception: An exception that is thrown, but never caught. This usually results in an error and the program ending or crashing.
- Try block: A set of code that might have an exception thrown in it.
Most programming languages use the terms “throw” and “catch.” Unfortunately Python doesn't. Python uses “raise” and “exception.” We introduce the throw/catch vocabulary here because they are the most prevalent terms in the industry.
The code for handling exceptions is simple. See the example below:
# Divide by zero try: x = 5/0 except: print("Error dividing by zero")
On line two is the try statement. Every indented line below it is part of the “try block.” There may be no unindented code below the try block that doesn't start with an except statement. The try statement defines a section of code that the code will attempt to execute.
If there is any exception that occurs during the processing of the code the execution will immediately jump to the “catch block.” That block of code is indented under the except statement on line 4. This code is responsible for handling the error.
A program may use exceptions to catch errors that occur during a conversion from text to a number. For example:
# Invalid number conversion try: x = int("fred") except: print ("Error converting fred to a number")
An exception will be thrown on line 3 because “fred” can not be converted to an integer. The code on line 5 will print out an error message.
Below is an expanded version on this example. It error-checks a user's input to make sure an integer is entered. If the user doesn't enter an integer, the program will keep asking for one. The code uses exception handling to capture a possible conversion error that can occur on line 5. If the user enters something other than an integer, an exception is thrown when the conversion to a number occurs on line 5. The code on line 6 that sets numberEntered to True will not be run if there is an exception on line 5.
numberEntered = False while numberEntered == False: numberString = input("Enter an integer: ") try: n = int(numberString) numberEntered = True except: print ("Error, invalid integer")
Files are particularly prone to errors during operations with them. A disk could fill up, a user could delete a file while it is being written, it could be moved, or a USB drive could be pulled out mid-operation. These types of errors may also be easily captured by using exception handling.
# Error opening file try: f = open("myfile.txt") except: print("Error opening file")
Multiple types of errors may be captured and processed differently. It can be useful to provide a more exact error message to the user than a simple “an error has occured.”
In the code below, different types of errors can occur from lines 5-8. By placing IOError after except on line 9, only errors regarding Input and Output (IO) will be handled by that code. Likewise line 11 only handles errors around converting values, and line 13 covers division by zero errors. The last exception handling occurs on line 15. Since line 15 does not include a particular type of error, it will handle any error not covered by the except blocks above. The “catch-all” except must always be last.
Line 1 imports the sys library which is used on line 16 to print the type of error that has occured.
import sys # Multiple errors try: f = open("myfile.txt") s = f.readline() i = int(s.strip()) x = 101/i except IOError: print ("I/O error") except ValueError: print ("Could not convert data to an integer.") except ZeroDivisionError: print ("Division by zero error") except: print ("Unexpected error:", sys.exc_info())
A list of built-in exceptions is available from this web address:
This shows how to save a high score between games. The score is stored in a file called high_score.txt.
# Sample Python/Pygame Programs # Simpson College Computer Science # http://programarcadegames.com/ # http://simpson.edu/computer-science/ # Default high score high_score = 0 # Try to read the high score from a file try: f = open("high_score.txt", "r") high_score = int(f.read() ) f.close() print ("The high score is",high_score) except: # Error reading file, no high score print("There is no high score yet.") # Get the score from the current game current_score = 0 try: # Ask the user for his/her score current_score = int(input ("What is your score? ")) except: # Error, can't turn what they typed into a number print("I don't understand what you typed.") # See if we have a new high score if current_score > high_score: print ("Yea! New high score!") # We do! Save to disk try: # Write the file to disk f = open("high_score.txt","w") f.write(str(current_score)) f.close() except: # Hm, can't write it. print("Too bad I couldn't save it.") else: print("Better luck next time.")
More information about an error can be pulled from the exception object. This object can be retrieved while catching an error using the as keyword. For example:
try: x = 5 / 0 except ZeroDivisionError as e: print(e)
The e variable points to more information about the exception that can be printed out. More can be done with exceptions objects, but unfortunately that is beyond the scope of this chapter.
Exceptions may be generated with the raise command. For example:
# Generating exceptions def getInput(): userInput = input("Enter something: ") if len(userInput) == 0: raise IOError("User entered nothing") getInput()
Try taking the code above, and add exception handling for the IOError raised.
It is also possible to create custom exceptions, but that is also
beyond the scope of this book. Curious readers may learn more by
Exceptions should not be used when if statements can just as easily handle the condition. Normal code should not raise exceptions when running the “happy path” scenario. Well-constructed try/catch code is easy to follow but code involving many exceptions and jumps in code to different handlers can be a nightmare to debug. (Once I was assigned the task of debugging code that read an XML document. It generated dozens of exceptions for each line of the file it read. It was incredibly slow and error-prone. That code should have never generated a single exception in the normal course of reading a file.)
- Define the following terms:
- Exception Handling
- Try block
- Catch block
- Unhandled exception
- Show how to modify the following code so that an error is printed if the number conversion is not successful:
user_input_string = input("Enter a number") user_value = int(user_input_string)
- What will the following code output?
x = 5 y = 0 print("A") try: print("B") a = x / y print("C") except: print("D") print("E") print(a)
- What will the following code output?
x = 5 y = 10 print("A") try: print("B") a = x / y print("C") except: print("D") print("E") print(a)
You are not logged in. Log in here and track your progress.
|About||Buy the Book||Help Translate||My College||My Twitter|