lecture15

lecture15 - Lecture 15: November 15, 2010 Exception...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Lecture 15 : November 15, 2010 Exception Handling, part 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 ! Exception handling How to recover from errors The try / except statement Exception objects Raising and catching exceptions Catch-all exception handlers
Background image of page 2
Caltech CS 1: Fall 2010 ! More on exception handling Catching exceptions outside of the function they were raised in ! The runtime stack Frames Tracebacks
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 ! Last time we saw this function: def sum_numbers_in_file (filename): sum_nums = 0 file = open(filename, 'r') for line in file: line = line.strip() sum_nums += int(line) file.close() return sum_nums
Background image of page 4
Caltech CS 1: Fall 2010 ! The purpose of sum_numbers_in_file is to take a filename and return a number ! The filename should exist and contain only numbers (one per line) ! However, errors can happen: the file may not exist the file may contain non-numbers the file may have blank lines ! How can we handle these errors?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 ! First, need to know what kinds of Python exceptions are raised when these errors happen ! If none of the error conditions exist, the function works fine ! If the file doesn't exist, an IOError exception is raised when attempting to read the file saw this last time ! What about other possible errors?
Background image of page 6
Caltech CS 1: Fall 2010 ! What if the line contains something other than numbers? ! For instance, imagine this is one section of a file being read: 11 43 10010 thirty-five 45
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 ! Let's run this file through the function: 11 # OK 43 # OK 10010 # OK thirty-five # Error: ValueError 45
Background image of page 8
Caltech CS 1: Fall 2010 ! The error comes when trying to execute this line: sum_nums += int(line) ! where line is 'thirty-five' ! The int function can't convert the string 'thirty-five' to an actual integer it's not that smart!
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 ! int('thirty-five') ! Gives this error message: ValueError : invalid literal for int() with base 10: 'thirty-five' ! Exception type: ValueError ! Data associated with the exception: invalid literal for int() with base 10: 'thirty-five' ! Gives more information about what caused the exception
Background image of page 10
Caltech CS 1: Fall 2010 ! OK, that takes care of non-numbers ! What about blank lines? ! Will end up having to compute sum_nums += int(line) ! where line is '' (ending newline has been stripped off by strip method call) ! So the function will compute: int('')
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 int('') ! results in: ValueError: invalid literal for int() with base 10: '' ! Python raises the same exception! ! In both cases, Python couldn't convert the contents of the line into a number that could be added to the sum ( sum_nums )
Background image of page 12
Caltech CS 1: Fall 2010 ! The function sum_numbers_in_file takes in an input argument, representing a file of numbers, one per line outputs the sum of all the numbers in the file may raise an IOError if the file doesn't exist may raise a ValueError if the file isn't formatted correctly (non-numbers or blank lines on some lines) !
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 71

lecture15 - Lecture 15: November 15, 2010 Exception...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online