1 / 11

Exception Handling and Files

Exception Handling and Files. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. File Access Problems. Files may not be usable by program for many reasons File program reads from may not exist Data in files may be wrong format File program writes to may not be accessible

shirin
Download Presentation

Exception Handling and Files

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. Exception Handling and Files CSIS 1595: Fundamentals of Programming and Problem Solving 1

  2. File Access Problems • Files may not be usable by program for many reasons • File program reads from may not exist • Data in files may be wrong format • File program writes to may not be accessible • Operating system may just not feel like it… • All of these cause exception if not handled

  3. Exception Handling Syntax try: code that might cause exception other code that should not be run if exception occurs except exception type: code to run if exception occurs …

  4. Key Python Exceptions • Key idea: Exception handling only way to detect and handle some types of errors • Parsing non-numeric values • Accessing nonexistent files

  5. Simple Example

  6. Exception Handling and Design • Possible actions if file can’t be opened: • Shut down program (if file necessary) • Prompt for another file (if user entered filename) • Must also give user option to quit

  7. Exception Handling and Design • Possible actions if file can’t be opened: • Use default values (often “empty document”)

  8. Exception Handling During Run • Failure can happen at any point in program • File on external drive opened successfully • User unplugs drive in middle of program… • Should enclose all file access in try block

  9. Nested Exceptions • Program reads numbers from file, prints total • Reading from file  IOError • Parsing numbers to add to total  ValueError • Key question: How to handle each? • Can’t open file  Quit program • Number can’t be parsed  Skip it and keep running (and print warning) • Often done by nesting try blocks • Outer block handles file problems, exiting program • Inner block handles parsing problems, jumping to next iteration of loop

  10. Nested Exceptions

  11. Handling Exceptions in Caller • What if exception nothandled inside function? • Immediately exit function • Exception then sent to caller who can then handle it • Simple way to force program exit for exceptions in functions

More Related