Skip to content

Instantly share code, notes, and snippets.

@dketov
Created December 12, 2011 15:49
Show Gist options
  • Save dketov/1467967 to your computer and use it in GitHub Desktop.
Save dketov/1467967 to your computer and use it in GitHub Desktop.
Исключения
# -*- 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
# -*- encoding: utf-8 -*-
"""
Вложенная обработка
"""
try:
try: 1/0
except:
print "caught an exception"
except ZeroDivisionError:
print "caught divide-by-0 attempt"
# -*- encoding: utf-8 -*-
"""
Инструкция finally
"""
try:
raise KeyboardInterrupt
finally:
print 'Goodbye, world!'
# -*- 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