1 / 11

Lecture 25

Lecture 25. Exceptions. Errors: IOError. If I try to open a file that doesn’t exist: >>> f = open("myfile.txt", "r") Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> f = open("myfile.txt", "r") IOError : [ Errno 2] No such file or directory: 'myfile.txt'

ava-huff
Download Presentation

Lecture 25

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 25 Exceptions

  2. Errors: IOError • If I try to open a file that doesn’t exist: • >>> f = open("myfile.txt", "r") • Traceback (most recent call last): • File "<pyshell#0>", line 1, in <module> • f = open("myfile.txt", "r") • IOError: [Errno 2] No such file or directory: 'myfile.txt' • >>>

  3. Errors: ZeroDivisionError • >>> x = 0 • >>> y = 4/x • Traceback (most recent call last): • File "<pyshell#4>", line 1, in <module> • y = 4/x • ZeroDivisionError: integer division or modulo by zero • >>>

  4. Errors: IndexError • >>> x = [3,5,7,9] • >>> y = x[4] • Traceback (most recent call last): • File "<pyshell#6>", line 1, in <module> • y = x[4] • IndexError: list index out of range • >>>

  5. Errors: TypeError • >>> x = "hello" • >>> y = x + 2 • Traceback (most recent call last): • File "<pyshell#8>", line 1, in <module> • y = x + 2 • TypeError: cannot concatenate 'str' and 'int' objects • >>>

  6. Errors: ValueError • >>> import math • >>> x = math.sqrt(-3) • Traceback (most recent call last): • File "<pyshell#10>", line 1, in <module> • x = math.sqrt(-3) • ValueError: math domain error

  7. Using an Exception: • #open file data.txt • def main(): • gotFile = False • while not(gotFile): • try: • name = raw_input("Please enter the filename: ") • f = open(name, "r") • gotFile = True • except IOError: • print "There is an error in the file name" • print "The file is open" • main()

  8. Basic form of an exception try: <body> except<ErrorType>: <handler> If no error type is given, it becomes the default. You can have multiple except statements to handle everything that can go wrong ErrorTypecan be just the name of the error or the Error name followed by a variable (example to come)

  9. # quadratic2.py # A program that computes the real roots of a quadratic equation. # Bad version using a simple if to avoid program crash # modified from code written by John Zelle import math def main(): print "This program finds the real solutions to a quadratic\n" a, b, c = input("Please enter the coefficients (a, b, c): ") discrim = b * b - 4 * a * c discRoot = math.sqrt(discrim) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print "\nThe solutions are:", root1, root2 main()

  10. def main(): print "This program finds the real solutions to a quadratic\n“ try: a, b, c = input("Please enter the coefficients (a, b, c): ") discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print "\nThe solutions are:", root1, root2 except ValueError, excObj: msg = str(excObj) if msg == "math domain error": print "No Real Roots" elifmsg == "unpack tuple of wrong size": print "You didn't give me the right number of coefficients." else: print "Something went wrong, sorry!" except NameError: print "\nYou didn't enter three numbers." except TypeError: print "\nYour inputs were not all numbers." except SyntaxError: print "\nYour input was not in the correct form. Missing comma(s), perhaps?" except: print "\nSomething went wrong, sorry!"

  11. Class problem • Compute the average of a list of positive numbers entered by the user. Use a loop to get the data and store it in a list. Tell the user to terminate the input by entering a zero. • Use the length of the list to compute the average. • Use an exception to take care of an empty list.

More Related