try:
x = int(input('Enter the first number: '))
y = int(input('Enter the second number: '))
print(x/y)
except ZeroDivisionError:
raise ValueError
except (TypeError, NameError) as e:
print(e)
except:
raise
| 类名 | 描述 |
|---|---|
| Exception | 所有异常类的基类 |
| AttributeError | 引用属性或赋值失败 |
| OSError | 操作系统不能执行 |
| IndexError | 使用序列中不存在的索引 |
| KeyError | 使用字典中不存在的键 |
| NameError | 找不到变量名称 |
| SyntaxError | 代码不正确 |
| TypeError | 将内置操作或函数用于类型不正确的对象 |
| ValueError | 将内置操作或函数用于类型正确但值不合适的对象时引发 |
| ZeroDivisionError | 除法或求模运算的第二个参数为0时引发 |
while True:
try:
x = int(input('Enter the first number: '))
y = int(input('Enter the second number: '))
print(x/y)
except:
print('Invalid input')
else:
break
本文章使用limfx的vsocde插件快速发布