1 / 12

Exception Handling

Exception Handling. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. What is an Exception?. Error : Fault in program that causes it to incorrectly implement a concept Example: math.sqrt function that given incorrect result Exception :

ziva
Download Presentation

Exception Handling

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

  2. What is an Exception? • Error: Fault in program that causes it to incorrectly implement a concept • Example: math.sqrt function that given incorrect result • Exception: Situation where concept itself undefined • Example: math.sqrt(-1) • Often caused by user error

  3. Examples of Exceptions

  4. Handling Exceptions • Key question: What should Python do if exception occurs? • Ignore problem (array out of bounds in C) • Shut down program • Print message… • What is best option? • Bank software  Save data, shut down, report error • Autopilot software  Display alert, but keep running!

  5. Handling Exceptions • Optimal solution: • Python should “notify” program about problem • Program should be given option of handling problem in whatever way is best for that circumstance Your code Python environment Code that causes exceptionx = int(“fred”) Code to handleexception call Internal code for intfunction detectsproblem and signalsexception to user signal

  6. 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 …

  7. Exception Handling Branching try: code that might cause exception other code in try block … other code in try block except exception type: code to run if exception rest of code exception occurs then go to rest of code

  8. Exception Handling Branching try: code that might cause exception other code in try block … other code in try block except exception type: code to run if exception rest of code no exception occurs, so skip the exception block

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

  10. Simple Example • Program inputs and squares number • Possible exception: User enters non-numeric value • Would cause ValueError

  11. Complex Example • 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)

  12. Complex Example

More Related