11
Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Embed Size (px)

Citation preview

Page 1: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Python Mini-CourseUniversity of Oklahoma

Department of Psychology

Lesson 19Handling Exceptions

6/09/09Python Mini-Course: Lesson 191

Page 2: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Lesson objectives

1. Understand how exceptions are generated and handled in Python

2. Use the raise statement to generate exceptions

3. Use the try…except statement to intercept and handle exceptions

4. List the common built-in exceptions

6/09/09Python Mini-Course: Lesson 192

Page 3: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

What is an exception?

Exceptions are run-time errorsWhenever the interpreter has a problem it notifies the user/programmer by raising an exception

6/09/09Python Mini-Course: Lesson 193

Page 4: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Handling exceptions

By default, the interpreter handles exceptions by stopping the program and printing an error message

However, we can override this behavior by catching the exception

6/09/09Python Mini-Course: Lesson 194

Page 5: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Example: nocatch.py

fin = open('bad_file')for line in fin: print linefin.close()

6/09/09Python Mini-Course: Lesson 195

Page 6: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Example: catch.py

try: fin = open('bad_file') for line in fin: print line fin.close()except: print 'Something went wrong.'

6/09/09Python Mini-Course: Lesson 196

Page 7: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Catching specific problems

There are a number of kinds of exceptions, including:IndexErrorEOFErrorKeyErrorSyntaxError

http://docs.python.org/library/exceptions.html#bltin-exceptions

6/09/09Python Mini-Course: Lesson 197

Page 8: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Example: catch2.py

try: fin = open('bad_file') for line in fin: print line fin.close()except IOError: print 'Something went wrong.'

6/09/09Python Mini-Course: Lesson 198

Page 9: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Error handling

Once you catch the error, you need to handle it1. Perform an alternate action2. Generate a customized error

message and gracefully end the program

6/09/09Python Mini-Course: Lesson 199

Page 10: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Raising exceptions

You can also create your own error generating and handling routines by raising an exception

Example:study147.py line 146

6/09/09Python Mini-Course: Lesson 1910

Page 11: Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

Exercise

Write a short program to divide two numbersInclude a routine to handle division by zero

6/09/09Python Mini-Course: Lesson 1911