try:
x = input('Enter x: ')
y = input('Enter y: ')
print x / y
except ZeroDivisionError as e:
print 'Error:',e
print 'hello world'
Enter x: 3
Enter y: 0
Error: integer division or modulo by zero
hello world
Enter x: 2
Enter y: 'a' # y 的输入是一个字符
----------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-209-d4666cfaefb4> in <module>()
2 x = input('Enter x: ')
3 y = input('Enter y: ')
----> 4 print x / y
5 except ZeroDivisionError as e:
6 print e
TypeError: unsupported operand type(s) for /: 'int' and 'str'
try:
x = input('Enter x: ')
y = input('Enter y: ')
print x / y
except ZeroDivisionError as e: # 处理 ZeroDivisionError 异常
print 'ZeroDivisionError:',e
except TypeError as e: # 处理 TypeError 异常
print 'TypeError:',e
print 'hello world'
Enter x: 3
Enter y: 'a'
TypeError: unsupported operand type(s) for /: 'int' and 'str'
hello world
Enter x: # 这里输入回车键
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
try:
x = input('Enter x: ')
y = input('Enter y: ')
print x / y
except ZeroDivisionError as e: # 捕获 ZeroDivisionError 异常
print 'ZeroDivisionError:',e
except TypeError as e: # 捕获 TypeError 异常
print 'TypeError:',e
except BaseException as e: # 捕获其他异常
print 'BaseException:',e
print 'hello world'
try:
x = input('Enter x: ')
y = input('Enter y: ')
print x / y
except ZeroDivisionError as e:
print 'ZeroDivisionError:',e
except TypeError as e:
print 'TypeError:',e
except BaseException as e:
print 'BaseException:',e
else:
print 'no error!'
print 'hello world'
Enter x: 6
Enter y: 2
3
no error!
hello world
try:
x = 1/0
print x
finally:
print 'DONE'
DONE
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: integer division or modulo by zero
try:
x = 1/0
print x
except ZeroDivisionError as e:
print 'ZeroDivisionError:',e
finally:
print 'DONE'
ZeroDivisionError: integer division or modulo by zero
DONE
try:
x = input('Enter x: ')
y = input('Enter y: ')
print x / y
except ZeroDivisionError as e:
print 'ZeroDivisionError:',e
except TypeError as e:
print 'TypeError:',e
except BaseException as e:
print 'BaseException:',e
raise # 使用 raise 抛出异常
else:
print 'no error!'
print 'hello world'
Enter x: # 这里输入回车键
BaseException: unexpected EOF while parsing (<string>, line 0)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
# 自定义异常类
class SomeError(Exception):
pass
try:
x = input('Enter x: ')
y = input('Enter y: ')
print x / y
except ZeroDivisionError as e:
print 'ZeroDivisionError:',e
except TypeError as e:
print 'TypeError:',e
except BaseException as e:
print 'BaseException:',e
raise SomeError('invalid value') # 抛出自定义的异常
else:
print 'no error!'
print 'hello world'
Enter x:
BaseException: unexpected EOF while parsing (<string>, line 0)
----------------------------------------------------------------------
SomeError Traceback (most recent call last)
<ipython-input-20-66060b472f91> in <module>()
12 except BaseException as e:
13 print 'BaseException:',e
---> 14 raise SomeError('invalid value')
15 else:
16 print 'no error!'
SomeError: invalid value