61a lecture 24
play

61A Lecture 24 Friday, November 1 Announcements 2 Announcements - PowerPoint PPT Presentation

61A Lecture 24 Friday, November 1 Announcements 2 Announcements Homework 7 due Tuesday 11/5 @ 11:59pm. 2 Announcements Homework 7 due Tuesday 11/5 @ 11:59pm. Project 1 composition revisions due Thursday 11/7 @ 11:59pm. 2 Heard on


  1. Raise Statements Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one. Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found KeyError -- A key wasn't found in a dictionary 9

  2. Raise Statements Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one. Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found KeyError -- A key wasn't found in a dictionary RuntimeError -- Catch-all for troubles during interpretation 9

  3. Raise Statements Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one. Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found KeyError -- A key wasn't found in a dictionary RuntimeError -- Catch-all for troubles during interpretation (Demo) 9

  4. Try Statements

  5. Try Statements 11

  6. Try Statements Try statements handle exceptions 11

  7. Try Statements Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ... 11

  8. Try Statements Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ... Execution rule: 11

  9. Try Statements Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ... Execution rule: The <try suite> is executed first. 11

  10. Try Statements Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ... Execution rule: The <try suite> is executed first. If, during the course of executing the <try suite> , an exception is raised that is not handled otherwise, and 11

  11. Try Statements Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ... Execution rule: The <try suite> is executed first. If, during the course of executing the <try suite> , an exception is raised that is not handled otherwise, and If the class of the exception inherits from <exception class> , then 11

  12. Try Statements Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ... Execution rule: The <try suite> is executed first. If, during the course of executing the <try suite> , an exception is raised that is not handled otherwise, and If the class of the exception inherits from <exception class> , then The <except suite> is executed, with <name> bound to the exception. 11

  13. Handling Exceptions 12

  14. Handling Exceptions Exception handling can prevent a program from terminating 12

  15. Handling Exceptions Exception handling can prevent a program from terminating >>> try: 12

  16. Handling Exceptions Exception handling can prevent a program from terminating >>> try: x = 1/0 12

  17. Handling Exceptions Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: 12

  18. Handling Exceptions Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) 12

  19. Handling Exceptions Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 12

  20. Handling Exceptions Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> 12

  21. Handling Exceptions Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x 12

  22. Handling Exceptions Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x 0 12

  23. Handling Exceptions Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x 0 Multiple try statements : Control jumps to the except suite of the most recent try statement that handles that type of exception. 12

  24. Handling Exceptions Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x 0 Multiple try statements : Control jumps to the except suite of the most recent try statement that handles that type of exception. (Demo) 12

  25. WWPD: What Would Python Do? How will the Python interpreter respond? 13

  26. WWPD: What Would Python Do? How will the Python interpreter respond? 13

  27. WWPD: What Would Python Do? How will the Python interpreter respond? def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try : return invert(x) except ZeroDivisionError as e: return str(e) 13

  28. WWPD: What Would Python Do? How will the Python interpreter respond? def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try : return invert(x) except ZeroDivisionError as e: return str(e) >>> invert_safe(1/0) 13

  29. WWPD: What Would Python Do? How will the Python interpreter respond? def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try : return invert(x) except ZeroDivisionError as e: return str(e) >>> invert_safe(1/0) >>> try : 13

  30. WWPD: What Would Python Do? How will the Python interpreter respond? def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try : return invert(x) except ZeroDivisionError as e: return str(e) >>> invert_safe(1/0) >>> try : ... invert_safe(0) 13

  31. WWPD: What Would Python Do? How will the Python interpreter respond? def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try : return invert(x) except ZeroDivisionError as e: return str(e) >>> invert_safe(1/0) >>> try : ... invert_safe(0) ... except ZeroDivisionError as e: 13

  32. WWPD: What Would Python Do? How will the Python interpreter respond? def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try : return invert(x) except ZeroDivisionError as e: return str(e) >>> invert_safe(1/0) >>> try : ... invert_safe(0) ... except ZeroDivisionError as e: ... print ('Handled!') 13

  33. WWPD: What Would Python Do? How will the Python interpreter respond? def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try : return invert(x) except ZeroDivisionError as e: return str(e) >>> invert_safe(1/0) >>> try : ... invert_safe(0) ... except ZeroDivisionError as e: ... print ('Handled!') >>> inverrrrt_safe(1/0) 13

  34. Interpreters

  35. Reading Scheme Lists 15

  36. Reading Scheme Lists A Scheme list is written as elements in parentheses: 15

  37. Reading Scheme Lists A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) 15

  38. Reading Scheme Lists A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive. 15

  39. Reading Scheme Lists A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive. (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) 15

  40. Reading Scheme Lists A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive. (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself. 15

  41. Reading Scheme Lists A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive. (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself. Parsers must validate that expressions are well-formed. 15

  42. Reading Scheme Lists A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive. (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself. Parsers must validate that expressions are well-formed. (Demo) http://composingprograms.com/projects/scalc/scheme_reader.py.html 15

  43. Reading Scheme Lists A Scheme list is written as elements in parentheses: A recursive (<element_0> <element_1> ... <element_n>) Scheme list Each <element> can be a combination or primitive. (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself. Parsers must validate that expressions are well-formed. (Demo) http://composingprograms.com/projects/scalc/scheme_reader.py.html 15

  44. Reading Scheme Lists A Scheme list is written as elements in parentheses: A recursive (<element_0> <element_1> ... <element_n>) Scheme list Each <element> can be a combination or primitive. (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself. Parsers must validate that expressions are well-formed. (Demo) http://composingprograms.com/projects/scalc/scheme_reader.py.html 15

  45. Reading Scheme Lists A Scheme list is written as elements in parentheses: A recursive (<element_0> <element_1> ... <element_n>) Scheme list Each <element> can be a combination or primitive. (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself. Parsers must validate that expressions are well-formed. (Demo) http://composingprograms.com/projects/scalc/scheme_reader.py.html 15

  46. Parsing

  47. Parsing 17

  48. Parsing A Parser takes text and returns an expression. 17

  49. Parsing A Parser takes text and returns an expression. Text Expression 17

  50. Parsing A Parser takes text and returns an expression. Lexical Text Expression analysis 17

  51. Parsing A Parser takes text and returns an expression. Lexical Text Tokens Expression analysis 17

  52. Parsing A Parser takes text and returns an expression. Lexical Syntactic Text Tokens Expression analysis analysis 17

  53. Parsing A Parser takes text and returns an expression. Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' ' (- 23)' ' (* 4 5.6))' 17

  54. Parsing A Parser takes text and returns an expression. Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' ' (- 23)' ' (* 4 5.6))' 17

  55. Parsing A Parser takes text and returns an expression. Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' ' (* 4 5.6))' 17

  56. Parsing A Parser takes text and returns an expression. Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' 17

  57. Parsing A Parser takes text and returns an expression. Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend