Python Programming - X. Exception Handling and Assertions

811
-1

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
811
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
143
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Python Programming - X. Exception Handling and Assertions

  1. 1. PYTHON PROGRAMMING Engr. Ranel O. Padon X. EXCEPTION HANDLING
  2. 2. PYTHON PROGRAMMING TOPICS I • Introduction to Python Programming II • Python Basics III • Controlling the Program Flow IV • Program Components: Functions, Classes, Packages, and Modules V • Sequences (List and Tuples), and Dictionaries VI • Object-Based Programming: Classes and Objects VII • Customizing Classes and Operator Overloading VIII • Object-Oriented Programming: Inheritance and Polymorphism IX • Randomization Algorithms X • Exception Handling and Assertions XI • String Manipulation and Regular Expressions XII • File Handling and Processing XIII • GUI Programming Using Tkinter
  3. 3. try except Defensive Programming else finally raise assert class Exception
  4. 4. http://www.newser.com/story/20282/china-atm-glitch-nets-man-life.html
  5. 5. http://www.newser.com/story/148427/atm-error-lets-man-withdraw-15m-gamble-it-away.html
  6. 6. INTRODUCTION Exception is an indication of a special event that occurs during a program’s execution. Exception indicates that, although the event can occur, the event occurs infrequently.
  7. 7. INTRODUCTION
  8. 8. INTRODUCTION When a Python script encounters a situation that it can't cope with, it raises an exception. An exception is a Python object that represents an error.
  9. 9. INTRODUCTION
  10. 10. INTRODUCTION
  11. 11. INTRODUCTION | Exceptions 1. 2. 3. 4. 5. 6. 7. 8. 9. Division by Zero Addition of two incompatible types. Accessing a file that is nonexistent. Accessing a nonexistent index of a sequence. Deleting a table in a disconnected database server. Withdrawing money greater than the available amount. Invalid input bearing angle. Negative perimeter distance. …
  12. 12. INTRODUCTION | Exceptions Resource Leak Aborting a program component could leave a file or a network connection in a state in which other programs are not able to acquire the resource.
  13. 13. EXCEPTION HANDLING
  14. 14. EXCEPTION HANDLING Advantages 1. handle the error - catching the exception - resolving the exception 2. continue processing as if no error had occured 3. programs are more clear, robust and more fault-tolerant
  15. 15. EXCEPTION HANDLING When no exceptions occur, exception-handling code incurs little or no performance penalties. Programs that implement exception handling operate more efficiently than programs that perform error handling throughout the program logic.
  16. 16. EXCEPTION HANDLING Brute-Force Error Handling
  17. 17. EXCEPTION HANDLING Brute-Force Error Handling it may work, but intermixing the logic of the program with the error-handling logic can make the program difficult to read, modify, maintain and debug—especially in large applications.
  18. 18. EXCEPTION HANDLING Exception handling enables the programmer to remove error-handling code from the “main line” of the program’s execution. This improves program clarity and enhances modifiability.
  19. 19. EXCEPTION HANDLING | Python Mechanism is similar with that used in Modula-3, C# and Java. Not all programming languages support exception handling.
  20. 20. EXCEPTION HANDLING
  21. 21. EXCEPTION HANDLING | try-except
  22. 22. EXCEPTION HANDLING | try-except Python uses try statements to enable exception handling. The try statement encloses statements that potentially cause exceptions. A try statement consists of keyword try, followed by a colon (:), followed by a suite of code in which exceptions may occur, followed by one or more clauses.
  23. 23. EXCEPTION HANDLING | try-except Immediately following the try suite may be one or more except clauses (also called except handlers). Each except clause specifies zero or more exception names that represent the type(s) of exceptions the except clause can handle.
  24. 24. EXCEPTION HANDLING | try-except If an exception occurs in a try suite, the try suite expires and program control transfers to the first matching except handler (if there is one) following the try suite.
  25. 25. EXCEPTION HANDLING | try-except an except handler always should specify the name of the exception to catch. an empty except handler should be used only for a default catch-all case.
  26. 26. EXCEPTION HANDLING | try-except If no exceptions occur in a try suite, the interpreter ignores the exception handlers for that try statement.
  27. 27. EXCEPTION HANDLING | try-except
  28. 28. EXCEPTION HANDLING | else After the last except clause, an optional else clause contains code that executes if the code in the try suite raised no exceptions.
  29. 29. EXCEPTION HANDLING | Summary
  30. 30. EXCEPTION HANDLING | Summary
  31. 31. EXCEPTION HANDLING | Summary
  32. 32. EXCEPTION HANDLING | Summary
  33. 33. EXCEPTION HANDLING | finally A try suite can be followed by zero except clauses; in that case, it must be followed by a finally clause. The code in the finally suite always executes, regardless of whether an exception occurs.
  34. 34. EXCEPTION HANDLING | finally Programs frequently request and release resources dynamically. Programs that obtain certain types of resources (such as files) sometimes must return those resources explicitly to the system to avoid resource leaks.
  35. 35. EXCEPTION HANDLING | finally The finally clause is an ideal location to place resource deallocation code for resources acquired.
  36. 36. EXCEPTION HANDLING | finally
  37. 37. EXCEPTION HANDLING | finally
  38. 38. EXCEPTION HANDLING | finally
  39. 39. EXCEPTION HANDLING | finally
  40. 40. EXCEPTION HANDLING | Exception Exceptions are objects of classes that inherit from class Exception. Programmer-defined exception classes should derive directly or indirectly from class Exception.
  41. 41. class Exception
  42. 42. class Exception (1/2)
  43. 43. class Exception (2/2)
  44. 44. EXCEPTION HANDLING | raise Raising/Throwing An Exception raise keyword Catching and handling exceptions enables a program to know when an error has occurred, then to take actions to minimize the consequences of that error.
  45. 45. EXCEPTION HANDLING | raise The raise statement may specify an argument or arguments that initialize the exception object. In this case, a comma follows the exception name, and the argument or a tuple of arguments follows the comma.
  46. 46. EXCEPTION HANDLING | raise Args
  47. 47. EXCEPTION HANDLING | raise Args
  48. 48. EXCEPTION HANDLING | Custom
  49. 49. EXCEPTION HANDLING | Custom
  50. 50. EXCEPTION HANDLING | Custom
  51. 51. EXCEPTION HANDLING | assert An assertion is a sanity-check that you can turn on/off when you are done with your testing of the program. Assertion is like a raise-if statement (or to be more accurate, a raise-if-not statement). An expression is tested, and if the result comes up false, an exception is raised.
  52. 52. EXCEPTION HANDLING | assert Programmers often place assertions at the start of a function to check for valid input, and after a function call to check for valid output.
  53. 53. EXCEPTION HANDLING | assert
  54. 54. EXCEPTION HANDLING | assert
  55. 55. EXCEPTION HANDLING | assert
  56. 56. EXCEPTION HANDLING | assert
  57. 57. EXCEPTION HANDLING | assert
  58. 58. EXCEPTION HANDLING | assert
  59. 59. EXCEPTION HANDLING
  60. 60. EXCEPTION HANDLING
  61. 61. REFERENCES  Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).  Disclaimer: Most of the images/information used here have no proper source citation, and I do not claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse and reintegrate materials that I think are useful or cool, then present them in another light, form, or perspective. Moreover, the images/information here are mainly used for illustration/educational purposes only, in the spirit of openness of data, spreading light, and empowering people with knowledge. 
  62. 62. Thank You!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×