Dealing with errors, Schemes and Mind Maps of Advanced Computer Programming

File <stdin>, line 1, in <module>. IOError: [Errno 2] No such file or directory: 'hello.txt'. >>> 55 + hello. Traceback (most recent call last):.

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 02/28/2023

ubimaiorminorcessat
ubimaiorminorcessat 🇺🇸

4.4

(17)

225 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
APRIL 9TH 2012
Dealing with errors
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Dealing with errors and more Schemes and Mind Maps Advanced Computer Programming in PDF only on Docsity!

A P R I L 9 T H^ 2 0 1 2

Dealing with errors

Examples of common run-time errors

— We are expecting the user to input an integer, but the

user inputs a sequence of letters.

— We want to read from a file, but the file is missing from

the directory.

— These are examples of run-time errors that we could plan

for and have our python program respond appropriately

and without just crashing.

— Python provides the try-except statements to help us

handle run-time errors.

Types of run-time errors

>>> fin = open("hello.txt", "r")
Traceback (most recent call last):
File "", line 1, in
IOError: [Errno 2] No such file or directory: 'hello.txt’
>>> 55 + "hello"
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for +: 'int' and 'str'

General Idea

— Every run-time error in Python has a type. — Examples of some other error types are: FloatingPointError, ImportError, IndentationError, KeyError, NameError, SyntaxError, TypeError. — Using the try-except statements we can plan for errors of certain types and respond to errors of different types differently.

How does the try-except statement work?

— First, the try clause (the statement(s) between the try and except keywords) is executed. — If no exception occurs, the except clause is skipped and execution of the try-except statement is finished. — If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then the execution continues after the try statement. — If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and we get one of the usual kinds of messages.

Second Example

while True: try: x = int(raw_input("Please type an integer: ")) y = int(raw_input("Please type an integer that is not zero: ")) print x/y break except ValueError: print "oops...that was not an integer. Try again!" print "Congratulations!! You've finally managed to input an integer."

The except keyword does not name error types while True: try: x = int(raw_input("Please type an integer: ")) y = int(raw_input("Please type an integer that is not zero: ")) print x/y break except: print "oops...there was some problem, try again!" print "Congratulations!! You've finally managed to input an integer."

Different except clauses can have different error types while True: try: x = int(raw_input("Please type an integer: ")) y = int(raw_input("Please type an integer that is not zero: ")) print x/y break except ValueError: print "oops...you did not input two integers. Try again." except ZeroDivisionError: print "oops...the second integer has to be non-zero. Try again."

print "Congratulations!! You've finally managed to input an integer."