lecture15 - Lecture 15 Exception Handling part 2 Caltech CS...

Info icon This 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
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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
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?
Image of page 5

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

View Full Document Right 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?
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
Image of page 7

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

View Full Document Right 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
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!
Image of page 9

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

View Full Document Right 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
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('')
Image of page 11

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

View Full Document Right 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 )
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) !
Image of page 13

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern