






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 12
This page cannot be seen from the preview
Don't miss anything!







A P R I L 9 T H^ 2 0 1 2
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.
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.
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."