Which called this line Caltech CS 1 Fall 2010 python dumbpy Traceback most

Which called this line caltech cs 1 fall 2010 python

This preview shows page 63 - 71 out of 71 pages.

Which called this line
Image of page 63
Caltech CS 1: Fall 2010 >>> python dumb.py Traceback (most recent call last): File "dumb.py", line 8, in <module> print func1(42) File "dumb.py", line 2, in func1 return (2 * func2(n)) File "dumb.py", line 4, in func2 return (3 * func3(n)) File "dumb.py", line 6, in func3 return (1 / 0) ! Which called this line
Image of page 64
Caltech CS 1: Fall 2010 >>> python dumb.py Traceback (most recent call last): File "dumb.py", line 8, in <module> print func1(42) File "dumb.py", line 2, in func1 return (2 * func2(n)) File "dumb.py", line 4, in func2 return (3 * func3(n)) File "dumb.py", line 6, in func3 return (1 / 0) ! Which called this line
Image of page 65
Caltech CS 1: Fall 2010 >>> python dumb.py Traceback (most recent call last): File "dumb.py", line 8, in <module> print func1(42) File "dumb.py", line 2, in func1 return (2 * func2(n)) File "dumb.py", line 4, in func2 return (3 * func3(n)) File "dumb.py", line 6, in func3 return (1 / 0) ! Which is where the error occurred
Image of page 66
Caltech CS 1: Fall 2010 ! At the end of the traceback, the exception type and error message (if any) is printed ! A traceback is only printed if the exception is not caught by a suitable try / except statement
Image of page 67
Caltech CS 1: Fall 2010 ! We showed how to write a function which calls sum_numbers_in_file and asks for a new filename when exceptions are raised ! If we were dealing with a large number of files, this wouldn't be practical ! Let's write a function to deal with this case ! When an exception occurs: an error message will be printed but execution will continue
Image of page 68
Caltech CS 1: Fall 2010 def sum_numbers_in_files (filenames): total = 0 for file in filenames: try: total += sum_numbers_in_file (filename) except IOError: print "Couldn't read file: %s" % filename except ValueError: print "Invalid line in file: %s" % filename return total
Image of page 69
Caltech CS 1: Fall 2010 ! When this function executes, exceptions in sum_numbers_in_file due to bad or missing files simply cause those files to be ignored ! More appropriate for a program which normally runs non-interactively ! In real program, error messages would probably be written out to a log file
Image of page 70
Caltech CS 1: Fall 2010 ! More object-oriented programming class "inheritance" ! Exceptions and classes creating your own exceptions raising exceptions explicitly the Exception base class
Image of page 71

You've reached the end of your free preview.

Want to read all 71 pages?

  • Winter '08
  • Pinkston,D
  • Subroutine, Call stack, Control flow, Caltech

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture