Created
December 12, 2011 15:49
-
-
Save dketov/1467967 to your computer and use it in GitHub Desktop.
Исключения
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Базовый синтаксис | |
""" | |
try: | |
num = float(raw_input("\nEnter a number: ")) | |
except(ValueError): | |
print "That was not a number!" | |
else: | |
print "You entered the number", num |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Вложенная обработка | |
""" | |
try: | |
try: 1/0 | |
except: | |
print "caught an exception" | |
except ZeroDivisionError: | |
print "caught divide-by-0 attempt" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Инструкция finally | |
""" | |
try: | |
raise KeyboardInterrupt | |
finally: | |
print 'Goodbye, world!' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Возбуждение исключения | |
""" | |
def err(): | |
raise IndexError | |
def aFunction(): | |
try: | |
err() | |
except IndexError: | |
print 'caught an index error!' | |
else: | |
print 'no error caught...' | |
if __name__ == '__main__': | |
aFunction() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment