1 / 11

Lesson 19 Handling Exceptions

Lesson 19 Handling Exceptions. Python Mini-Course University of Oklahoma Department of Psychology. Lesson objectives. Understand how exceptions are generated and handled in Python Use the raise statement to generate exceptions

gstouffer
Download Presentation

Lesson 19 Handling Exceptions

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. Lesson 19Handling Exceptions Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Lesson 19

  2. Lesson objectives • Understand how exceptions are generated and handled in Python • Use the raise statement to generate exceptions • Use the try…except statement to intercept and handle exceptions • List the common built-in exceptions Python Mini-Course: Lesson 19

  3. What is an exception? • Exceptions are run-time errors • Whenever the interpreter has a problem it notifies the user/programmer by raising an exception Python Mini-Course: Lesson 19

  4. Handling exceptions • By default, the interpreter handles exceptions by stopping the program and printing an error message • However, we can override this behavior by catching the exception Python Mini-Course: Lesson 19

  5. Example: nocatch.py fin = open('bad_file') for line in fin: print line fin.close() Python Mini-Course: Lesson 19

  6. Example: catch.py try: fin = open('bad_file') for line in fin: print line fin.close() except: print 'Something went wrong.' Python Mini-Course: Lesson 19

  7. Catching specific problems • There are a number of kinds of exceptions, including: • IndexError • EOFError • KeyError • SyntaxError • http://docs.python.org/library/exceptions.html#bltin-exceptions Python Mini-Course: Lesson 19

  8. Example: catch2.py try: fin = open('bad_file') for line in fin: print line fin.close() except IOError: print 'Something went wrong.' Python Mini-Course: Lesson 19

  9. Error handling • Once you catch the error, you need to handle it • Perform an alternate action • Generate a customized error message and gracefully end the program Python Mini-Course: Lesson 19

  10. Raising exceptions • You can also create your own error generating and handling routines by raising an exception • Example: • study147.py line 146 Python Mini-Course: Lesson 19

  11. Exercise • Write a short program to divide two numbers • Include a routine to handle division by zero Python Mini-Course: Lesson 19

More Related