asserts and error handling announcements for today
play

Asserts and Error Handling Announcements for Today Reading - PowerPoint PPT Presentation

Lecture 11 Asserts and Error Handling Announcements for Today Reading Assignments Reread Chapter 3 Finishing Assignment 1 10.0-10.2, 10.4-10.6 for Tue We are going to score it Get one more chance Sun. Prelim, Oct 17 th


  1. Lecture 11 Asserts and Error Handling

  2. Announcements for Today Reading Assignments • Reread Chapter 3 • Finishing Assignment 1 • 10.0-10.2, 10.4-10.6 for Tue § We are going to score it § Get one more chance Sun. • Prelim, Oct 17 th 7:30-9:00 • Assignment 2 in progress § Material up October 8th § Will grade it by Friday § Study guide next week § Solutions posted by Friday • Conflict with Prelim time? § Submit to Prelim 1 Conflict • Assignment 3 due next week assignment on CMS § Before you leave for break § Do not submit if no conflict § Same “length” as A1 10/3/19 Asserts & Error Handling 2

  3. Using Color Objects in A3 • New classes in introcs id1 c id1 § RGB, CMYK, and HSV RGB r 128 red 128 • Each has its own attributes green 0 § RGB : red , blue , green § CMYK : cyan , magenta , blue 0 yellow , black § HSV : hue , saturation , value >>> import introcs • Attributes have invariants >>> c = introcs .RGB(128,0,0) § Limits the attribute values >>> r = c.red § Example: red is int in 0..255 >>> c.red = 500 # out of range § Get an error if you violate AssertionError: 500 outside [0,255] 10/3/19 Asserts & Error Handling 3

  4. Using Color Objects in A3 • New classes in introcs id1 c id1 § RGB, CMYK, and HSV RGB r 128 red 128 • Each has its own attributes green 0 § RGB : red , blue , green Constructor function. § CMYK : cyan , magenta , blue 0 To make a new color. yellow , black § HSV : hue , saturation , value >>> import introcs • Attributes have invariants >>> c = introcs .RGB(128,0,0) § Limits the attribute values >>> r = c.red § Example: red is int in 0..255 Accessing >>> c.red = 500 # out of range Attribute § Get an error if you violate AssertionError: 500 outside [0,255] 10/3/19 Asserts & Error Handling 4

  5. Recall: The Call Stack • Functions are stacked § Cannot remove one above Frame 1 calls w/o removing one below Frame 2 § Sometimes draw bottom up calls (better fits the metaphor) Frame 3 calls • Stack represents memory Frame 4 as a high water mark calls § Must have enough to keep the Frame 6 Frame 5 entire stack in memory § Error if cannot hold stack 10/3/19 Asserts & Error Handling 5

  6. Error Messages Not An Error Message An Error Message ZeroDivisionError: division by zero Traceback (most recent call last): File "error.py", line 41, in <module> print(function_1(1,0)) Everything starting File "error.py", line 16, in function_1 with the Traceback return function_2(x,y) File "error.py", line 26, in function_2 return function_3(x,y) File "error.py", line 36, in function_3 return x/y ZeroDivisionError: division by zero 10/3/19 Asserts & Error Handling 6

  7. Errors and the Call Stack # error.py def function_1(x,y): return function_2(x,y) calls def function_2(x,y): return function_3(x,y) calls def function_3(x,y): return x/y # crash here calls if __name__ == '__main__': print(function_1(1,0)) 10/3/19 Asserts & Error Handling 7

  8. Errors and the Call Stack # error.py Crashes produce the call stack: Traceback (most recent call last): def function_1(x,y): File "error.py", line 20, in <module> return function_2(x,y) print(function_1(1,0)) File "error.py", line 8, in function_1 def function_2(x,y): return function_2(x,y) return function_3(x,y) File "error.py", line 12, in function_2 return function_3(x,y) def function_3(x,y): File "error.py", line 16, in function_3 return x/y # crash here return x/y if __name__ == '__main__': Make sure you can see print(function_1(1,0)) line numbers in Atom. 10/3/19 Asserts & Error Handling 8

  9. Errors and the Call Stack # error.py Crashes produce the call stack: Script code. Global space Traceback (most recent call last): def function_1(x,y): File "error.py", line 20, in <module> return function_2(x,y) print(function_1(1,0)) File "error.py", line 8, in function_1 def function_2(x,y): return function_2(x,y) return function_3(x,y) File "error.py", line 12, in function_2 return function_3(x,y) def function_3(x,y): File "error.py", line 16, in function_3 return x/y # crash here return x/y if __name__ == '__main__': Where error occurred Make sure you can see print function_1(1,0) (or where was found) line numbers in Atom. 10/3/19 Asserts & Error Handling 9

  10. Recall: Assigning Responsibility Developer 1 Developer 2 Function BROKEN Calls Defines Whose fault is it? Who must fix it? 10/3/19 Asserts & Error Handling 10

  11. Determining Responsibility Traceback (most recent call last): def function_1(x,y): """Returns: result of function_2 File "error1.py", line 32, in <module> Precondition: x, y numbers""" print(function_1(1,0)) return function_2(x,y) File "error1.py", line 18, in function_1 def function_2(x,y): return function_2(x,y) """Returns: x divided by y File "error1.py", line 28, in function_2 Precondition: x, y numbers""" return x/y return x/y ZeroDivisionError: division by zero Where is the error? print(function_1(1,0)) 10/3/19 Asserts & Error Handling 11

  12. Approaching the Error Message Traceback (most recent call last): • Start from the top • Look at function call File "error1.py", line 32, in <module> print(function_1(1,0)) § Examine arguments § (Print if you have to) File "error1.py", line 18, in function_1 return function_2(x,y) § Verify preconditions File "error1.py", line 28, in function_2 • Violation? Error found return x/y § Else go to next call § Continue until bottom ZeroDivisionError: division by zero 10/3/19 Asserts & Error Handling 12

  13. Determining Responsibility Traceback (most recent call last): def function_1(x,y): """Returns: result of function_2 A File "error1.py", line 32, in <module> Precondition: x, y numbers""" print(function_1(1,0)) return function_2(x,y) B File "error1.py", line 18, in function_1 def function_2(x,y): return function_2(x,y) """Returns: x divided by y C File "error1.py", line 28, in function_2 Precondition: x, y numbers""" return x/y return x/y ZeroDivisionError: division by zero Where is the error? print(function_1(1,0)) 10/3/19 Asserts & Error Handling 13

  14. Determining Responsibility Traceback (most recent call last): def function_1(x,y): """Returns: result of function_2 File "error1.py", line 32, in <module> Precondition: x, y numbers""" print(function_1(1,0)) return function_2(x,y) File "error1.py", line 18, in function_1 def function_2(x,y): return function_2(x,y) """Returns: x divided by y File "error1.py", line 28, in function_2 Precondition: x, y numbers""" return x/y Error! return x/y ZeroDivisionError: division by zero print(function_1(1,0)) 10/3/19 Asserts & Error Handling 14

  15. Determining Responsibility Traceback (most recent call last): def function_1(x,y): """Returns: result of function_2 A File "error1.py", line 32, in <module> Precondition: x, y numbers""" print(function_1(1,0)) return function_2(x,y) B File "error1.py", line 18, in function_1 def function_2(x,y): return function_2(x,y) """Returns: x divided by y C File "error1.py", line 28, in function_2 Precondition: x, y numbs, y > 0""" return x/y return x/y ZeroDivisionError: division by zero Where is the error? print(function_1(1,0)) 10/3/19 Asserts & Error Handling 15

  16. Determining Responsibility Traceback (most recent call last): def function_1(x,y): """Returns: result of function_2 File "error1.py", line 32, in <module> Precondition: x, y numbers""" print(function_1(1,0)) return function_2(x,y) File "error1.py", line 18, in function_1 def function_2(x,y): return function_2(x,y) Error! """Returns: x divided by y File "error1.py", line 28, in function_2 Precondition: x, y numbs, y > 0""" return x/y return x/y ZeroDivisionError: division by zero print(function_1(1,0)) 10/3/19 Asserts & Error Handling 16

  17. Determining Responsibility Traceback (most recent call last): def function_1(x,y): """Returns: result of function_2 File "error1.py", line 32, in <module> Precondition: x, y numbs, y > 0""" print(function_1(1,0)) Error! return function_2(x,y) File "error1.py", line 18, in function_1 def function_2(x,y): return function_2(x,y) """Returns: x divided by y File "error1.py", line 28, in function_2 Precondition: x, y numbs, y > 0""" return x/y return x/y ZeroDivisionError: division by zero print(function_1(1,0)) 10/3/19 Asserts & Error Handling 17

Recommend


More recommend