#多个execpt捕获异常
try:
x = input("enter the first number:");
y = input("enter the second number:");
print(int(x)/int(y));
except ZeroDivisionError:
print("the second number can't zero");
except TypeError:
print("that wasn't a number.");
#一个块捕获异常
try:
x = input("enter the first number:");
y = input("enter the second number:");
print(int(x)/int(y));
except (ZeroDivisionError, TypeError, NameError):
print("your numbers were bogus.");
#捕获异常
try:
x = input("enter the first number:");
y = input("enter the second number:");
print(int(x)/int(y));
except (ZeroDivisionError, TypeError, NameError)as e:
print(e);
#捕获所有的异常
try:
x = input("enter the first number:");
y = input("enter the second number:");
print(int(x)/int(y));
except:
print("something wrong happend!");
#异常处理1
try:
x = input("enter the first number:");
y = input("enter the second number:");
print(int(x)/int(y));
except:
print("something wrong happend!");
else:
print("this ok!");
#异常处理2
while(True):
try:
x = input("enter the first number:");
y = input("enter the second number:");
print(int(x)/int(y));
except:
print("something wrong happend!");
else:
break;
#获取所有异常信息
while(True):
try:
x = input("enter the first number:");
y = input("enter the second number:");
print(int(x)/int(y));
except Exception as e:
print(e);
else:
break;
#finally子句,无论是否发生异常都会执行finally子句
while(True):
try:
x = input("enter the first number:");
y = input("enter the second number:");
print(int(x)/int(y));
except Exception as e:
print(e);
else:
break;
finally:
print("cleaning up!");
python学习之异常
最新推荐文章于 2024-06-22 21:35:28 发布