Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1.

Slides:



Advertisements
Similar presentations
Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handling – improves program clarity by removing.
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Lecture 07 – Exceptions.  Understand the flow of control that occurs with exceptions  try, except, finally  Use exceptions to handle unexpected runtime.
Debugging Introduction to Computing Science and Programming I.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Exceptions COMPSCI 105 S Principles of Computer Science.
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 2 Fundamentals of Programming Languages 4/5/09 Python Mini-Course: Day.
Object Oriented Programming
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
Exceptions 2 COMPSCI 105 S Principles of Computer Science.
Python – May 17 Recap lab Multidimensional list Exceptions Commitment: quiz tomorrow.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 13 Case study: Word play 05/02/09 Python Mini-Course: Day 4 – Lesson.
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
Exceptions COMPSCI 105 SS 2015 Principles of Computer Science.
Making Good Code AKA: So, You Wrote Some Code. Now What? Ray Haggerty July 23, 2015.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 18 Using text files to share data with other programs 5/07/09 Python Mini-Course:
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
FT228/3 Web Development Error processing. Introduction READ Chapter 9 of Java Server Pages from O’reilly 2 nd Edition Need to be able to 1) Diagnose and.
ICS 313: Programming Language Theory Chapter 14: Exceptions.
Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2.
Firoze Abdur Rakib. Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning.
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Lecture 4 Python Basics Part 3.
Python Built-in Exceptions Data Fusion Albert Esterline Source:
Lecture 07 – Exceptions.  Understand the flow of control that occurs with exceptions  try, except, finally  Use exceptions to handle unexpected runtime.
Python Exceptions and bug handling Peter Wad Sackett.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Head First Python: Ch 3. Files and Exceptions: Dealing with Errors Aug 26, 2013 Kyung-Bin Lim.
Exception Handling How to handle the runtime errors.
Exception Handlings Lecture Week 10. Topics Overview Advantages Types Examples.
Introduction to Exceptions in Java CS201, SW Development Methods.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
Python: Exception Handling Damian Gordon. Exception Handling When an error occurs in a program that causes the program to crash, we call that an “exception”
COMPSCI 107 Computer Science Fundamentals
Exceptions in Python Error Handling.
George Mason University
Exceptions and files Taken from notes by Dr. Neil Moore
Python’s Errors and Exceptions
Exceptions and files Taken from notes by Dr. Neil Moore
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
ERRORS AND EXCEPTIONS Errors:
(Oops! When things go wrong)
Advanced Python Concepts: Exceptions
Advanced Python Concepts: Exceptions
Exception Handling COSC 1323.
Python Exceptions and bug handling
UW CSE 190p Section 7/19, Summer 2012 Dun-Yu Hsiao.
Dealing with Runtime Errors
Presentation transcript:

Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1

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

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

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 6/09/09 Python Mini-Course: Lesson 19 4

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

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

Catching specific problems There are a number of kinds of exceptions, including: IndexError EOFError KeyError SyntaxError ons.html#bltin-exceptions 6/09/09 Python Mini-Course: Lesson 19 7

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

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

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

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