lecture14

lecture14 - Caltech CS 1 Fall 2010 Lecture 14 Exception...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Caltech CS 1: Fall 2010 Lecture 14 : November 10, 2010 Exception Handling, part 1 Caltech CS 1: Fall 2010 Classes and graphical objects • building a Square class • methods for scaling, moving, deleting squares Caltech CS 1: Fall 2010 Exception handling • How to recover from errors • The try / except statement • Exception objects • Raising and catching exceptions • Catch-all exception handlers Caltech CS 1: Fall 2010 This is not a "fun" lecture We won't see cool new ways to accomplish neat stuff in a few lines of code Instead, we will do two things: 1. learn how to handle errors correctly 2. understand better how Python works internally Caltech CS 1: Fall 2010 Many possible errors can occur while running a Python program • division by zero • accessing a non-existent index of a list • accessing a non-existent key in a dictionary • trying to store a new value into a tuple • trying to open a file (for reading) that does not exist • etc. Caltech CS 1: Fall 2010 How should errors be handled? Consider: >>> a = 1 / 0 This will not give a legal value • Python doesn't have infinitely large integers How should this error be handled? • Many possibilities... Caltech CS 1: Fall 2010 Could ignore the error and just continue • Called "silent failure" • Problem: often will cause the program to crash • (common in the C programming language) • Not an effective way to proceed! • Don't know why error occurred or where it occurs Caltech CS 1: Fall 2010 Could halt the program • Advantage: prevents a crash • Problems: • Too drastic (some errors can be recovered from!) • Still don't know why error occurred or where it occurred Caltech CS 1: Fall 2010 Could print informative error message and halt the program • Advantages: • prevents a crash • know why error happened (but maybe not where) • Problems: • Too drastic (some errors can be recovered from!) • Still don't know where error occurred Caltech CS 1: Fall 2010 Could print informative error message stating why and where the error occurred and halt the program • Advantages: • prevents a crash • know why and where error happened • Problems: • Too drastic (some errors can be recovered from!) This is what Python does by default Caltech CS 1: Fall 2010 >>> a = 1 / 0 Traceback (most recent call first): File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero Two components to this error message Caltech CS 1: Fall 2010 >>> a = 1 / 0 Traceback (most recent call first): File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero Error message states what kind of error occurred and what it means Caltech CS 1: Fall 2010 >>> a = 1 / 0 Traceback (most recent call first): File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero Traceback states where the error occurred We'll look into this more deeply next lecture Caltech CS 1: Fall 2010...
View Full Document

This note was uploaded on 02/22/2011 for the course CS 1 taught by Professor Pinkston,d during the Fall '08 term at Caltech.

Page1 / 60

lecture14 - Caltech CS 1 Fall 2010 Lecture 14 Exception...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online