1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handling – improves program clarity by removing.

Slides:



Advertisements
Similar presentations
What type of data can a variable hold?
Advertisements

Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handling – improves program clarity by removing.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Head First Python Chapter 4 Persistence: Saving Data to Files SNU IDB Lab.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handler catches a raised/thrown exception try.
Files and Streams Python views files as sequential streams of bytes Each file ends with an end-of-file marker Opening a file creates an object associated.
 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.
Bordoloi and Bock EXCEPTIONS. Bordoloi and Bock Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
CIS 270—Application Development II Chapter 13—Exception Handling.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
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.
General Programming Introduction to Computing Science and Programming I.
Exceptions 2 COMPSCI 105 S Principles of Computer Science.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Guide to Programming with Python Chapter Seven (Part 1) Files and Exceptions: The Trivia Challenge Game.
Input, Output, and Processing
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Functions Chapter 4 Python for Informatics: Exploring Information
Python Conditionals chapter 5
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
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)
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
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.
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.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Exception Handling How to handle the runtime errors.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
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”
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
Introduction to Python
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Why exception handling in C++?
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Python - Functions.
Exceptions and files Taken from notes by Dr. Neil Moore
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Exceptions and files Taken from notes by Dr. Neil Moore
ERRORS AND EXCEPTIONS Errors:
(Oops! When things go wrong)
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Python Syntax Errors and Exceptions
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
By Ryan Christen Errors and Exceptions.
Topics Introduction to File Input and Output
Dealing with Runtime Errors
Presentation transcript:

1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handling – improves program clarity by removing error-handling code from the “main line” of the program – Improves a program’s fault tolerance Exception handler catches a raised/thrown exception try statement encloses code that potentially cause exceptions raise statement indicates an exception occurred

number1 = raw_input( "Enter numerator: " ) number2 = raw_input( "Enter denominator: " ) # attempt to convert and divide values try: number1 = float( number1 ) number2 = float( number2 ) result = number1 / number2 # float raises a ValueError exception except ValueError: print "You must enter two numbers" # division by zero raises a ZeroDivisionError exception except ZeroDivisionError: print "Attempted to divide by zero" # else clause's suite executes if try suite raises no exceptions else: print "%.3f / %.3f = %.3f" % ( number1, number2, result ) User might raise a ValueError by entering a string that cannot be converted to float Division by zero raises ZeroDivisionError exception

Files and Streams Python views files as sequential streams of bytes Each file ends with an end-of-file marker Opening a file creates an object associated with a stream n-1... end-of-file marker Fig Python’s view of a file of n bytes.

4 Files and Streams Three file streams created when Python program executes – sys.stdin (standard input stream) – sys.stdout (standard output stream) – sys.stderr (standard error stream) Defaulted to keyboard, screen and screen, respectively ( raw_input uses stdin, print uses stdout ) Redirect: print >> file, “Yes sir we have no bananas”

5 Methods for reading a file

6 CD management cd.py

7 Implicitly calling the __str__ method of the cd class

8 Save cd collection to a file, line by line cd.py

9 File cdsave.dat cdtest.py Test Prince / Purple Rain (rating: 7.4) Oscar Peterson / Night Train (rating: 8.0) Beatles / Sgt. Pepper (rating: 9.6) Program output Prince Purple Rain 7.4 Oscar Peterson Night Train 8.0 Beatles Sgt. Pepper 9.6

10 Load cd collection from file, line by line Create new CD object and add to collection cd.py Remove the trailing newline from line

11 Testing the load method >>> from cd import CD, CDcollection >>> cds = CDcollection() >>> cds.load( 'cdsave.dat') >>> print cds Prince / Purple Rain (rating: 7.4) Oscar Peterson / Night Train (rating: 8.0) Beatles / Sgt. Pepper (rating: 9.6)

12 Extending class definition with cd comments Write each comment on a line of its own, with a suitable number of spaces cd2.py

13 Prince / Purple Rain (rating: 7.4) [Groundbreaking!] Oscar Peterson / Night Train (rating: 8.0) [Got it in a Starbucks in West L.A.] [Good album, especially the Gershwin songs.] Beatles / Sgt. Pepper (rating: 9.6) [Groundbreaking AND evergreen!!] cd2test.py

14 New save method Write each comment on a line of its own after artist, title and rating cd2.py

15 New load method..? Which attributes are which in the saved file?? No longer 3 lines per cd  Change save method or Write super-intelligent load method Prince Purple Rain 7.4 Groundbreaking! Oscar Peterson Night Train 8.0 Got it in a Starbucks in West L.A. Good album, especially the Gershwin songs. Beatles Sgt. Pepper 9.6 Groundbreaking AND evergreen!!

16 New load/save methods using module cPickle cd_pickle.py Write entire object (list of CD objects) to file Easy to load object back into memory if you’re completely sure what kind of object the file contains

17 Testing the pickle save method cd_pickletest.py.. S'Purple Rain' p6 sS'comments' p7 (lp8 S'Groundbreaking!' p9 asS'artist' p10 S'Prince' p11 sba(icd_pickle CD.. Excerpt from file cd_picklesave.dat

18 Testing the load method cd_pickletest_load.py Prince / Purple Rain (rating: 7.4) [Groundbreaking!] Oscar Peterson / Night Train (rating: 8.0) [Got it in a Starbucks in West L.A.] [Good album, especially the Gershwin songs.] Beatles / Sgt. Pepper (rating: 9.6) [Groundbreaking AND evergreen!!] Beatles / A Hard Day's Night (rating: 6.8) [First album with all Lennon/McCartney songs] We need a CDcollection object for which we can call the load method to retrieve the pickled object