All programs will create errors at some point. When working with files, you'll encounter problems with the data in the file not following the standard so our code cannot read it correctly. When communicating with web services we may find that their server is down and not responding to our requests. Because of this problems and others, we must prepare our programs to handle "error conditions". In Python this is referred to as "Exception Handling".
Like many things in Python, exception handling is very easy. You just need to put your code into a "try/except" block. Your code goes between the "try:" and "except:" keywords and then you put the error message after the "except:". If an error occurs in the code in the "try" section, then the code in the "except" will be called. Try the example below that contains a "divide-by-zero" error.
try: y=12/0 except: print "* An Error Has Occurred!"
This is actually very important for when we start calling external programs and more complex libraries. If they generate an exception, the program will simple stop and you'll have no idea why. If you capture the "exception" then you'll know what happened. If you're not sure which part of the code generated the exception, you can just add more "try/except" blocks around suspicious code until you isolate the problem.
For now we'll be adding "print" statements to our code these will only appear when we are running in the IDE or in "command line mode" (more on this later). Soon, we'll learn how to display errors in "alert boxes" similar to those you have seen in most software programs.
Over time, you'll add more and more try/except blocks to your programs. You'll always want to have one around the "top-level" of your code. Then, add additional try/except blocks to catch errors where the user will need additional information. As an example, you may write a function that reads a specific type of file such as a text file. You could add a try/catch block that indicates "Sorry, there was a problem reading from the file <FilePath>". This will be friendly than many of the standard errors and you can provide additional clues for the user to fix the problem.
Sometimes, you'll want to display a simple error message for the user. Other times, you'll want to access the error that occurred and add some additional text to it. The code below will get the exception that occurred and print it in a more freindly string.
try: x="hi"+1 except Exception as TheException: print("Sorry, an error has occurred: "+format(TheException))
Note: From now on, all your programs MUST contain their top-level code in a "try/except" block.
You'll want to be able to find the exact line of code that generated an exception in large programs. This can be done by getting the "stack trace" from the system info. The code below will import the "sys" and "traceback" packages and then print out the stack trace and other information when an exception is generated.
import sys import traceback try: x="hi"+1 except Exception as TheException: print("Sorry, an error has occurred: "+format(TheException)) exc_type, exc_value, exc_traceback =sys.exc_info() print(exc_type) print(exc_value) traceback.print_tb(exc_traceback, limit=10)
Python Documentation: Errors and Exceptions
Python Documentation: Built in Exceptions
Python Documentation: Traceback
© Copyright 2018 HSU - All rights reserved.