exceptions announcements exceptions today s topic
play

Exceptions Announcements Exceptions Today's Topic: Handling Errors - PowerPoint PPT Presentation

Exceptions Announcements Exceptions Today's Topic: Handling Errors 4 Today's Topic: Handling Errors Sometimes, computer programs behave in non-standard ways 4 Today's Topic: Handling Errors Sometimes, computer programs behave in


  1. Exceptions

  2. Announcements

  3. Exceptions

  4. Today's Topic: Handling Errors 4

  5. Today's Topic: Handling Errors Sometimes, computer programs behave in non-standard ways 4

  6. Today's Topic: Handling Errors Sometimes, computer programs behave in non-standard ways • A function receives an argument value of an improper type 4

  7. Today's Topic: Handling Errors Sometimes, computer programs behave in non-standard ways • A function receives an argument value of an improper type • Some resource (such as a file) is not available 4

  8. Today's Topic: Handling Errors Sometimes, computer programs behave in non-standard ways • A function receives an argument value of an improper type • Some resource (such as a file) is not available • A network connection is lost in the middle of data transmission 4

  9. Today's Topic: Handling Errors Sometimes, computer programs behave in non-standard ways • A function receives an argument value of an improper type • Some resource (such as a file) is not available • A network connection is lost in the middle of data transmission Grace Hopper's Notebook, 1947, Moth found in a Mark II Computer 4

  10. Exceptions 5

  11. Exceptions A built-in mechanism in a programming language to declare and respond to exceptional conditions 5

  12. Exceptions A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs 5

  13. Exceptions A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting 5

  14. Exceptions A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace 5

  15. Exceptions A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Mastering exceptions: 5

  16. Exceptions A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Mastering exceptions: Exceptions are objects! They have classes with constructors. 5

  17. Exceptions A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Mastering exceptions: Exceptions are objects! They have classes with constructors. They enable non-local continuation of control 5

  18. Exceptions A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Mastering exceptions: Exceptions are objects! They have classes with constructors. They enable non-local continuation of control If f calls g and g calls h , exceptions can shift control from h to f without waiting for g to return. 5

  19. Exceptions A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Mastering exceptions: Exceptions are objects! They have classes with constructors. They enable non-local continuation of control If f calls g and g calls h , exceptions can shift control from h to f without waiting for g to return. (Exception handling tends to be slow.) 5

  20. Raising Exceptions

  21. Assert Statements Assert statements raise an exception of type AssertionError 7

  22. Assert Statements Assert statements raise an exception of type AssertionError assert <expression>, <string> 7

  23. Assert Statements Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag; "O" stands for optimized 7

  24. Assert Statements Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag; "O" stands for optimized python3 -O 7

  25. Assert Statements Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag; "O" stands for optimized python3 -O Whether assertions are enabled is governed by a bool __debug__ 7

  26. Assert Statements Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag; "O" stands for optimized python3 -O Whether assertions are enabled is governed by a bool __debug__ (Demo) 7

  27. Raise Statements 8

  28. Raise Statements Exceptions are raised with a raise statement 8

  29. Raise Statements Exceptions are raised with a raise statement raise <expression> 8

  30. Raise Statements Exceptions are raised with a raise statement raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one 8

  31. 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!') 8

  32. 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 8

  33. 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 8

  34. 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 8

  35. 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 RecursionError -- Too many recursive calls 8

  36. 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 RecursionError -- Too many recursive calls (Demo) 8

  37. Try Statements

  38. Try Statements 10

  39. Try Statements Try statements handle exceptions 10

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

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

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

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