?
?
?
?
try:
a = int(input("num1:")) // int(input("num2:"))
except BaseException as e:
print(e)
f1 = open("D:/pythonStudy/day09/a.txt","a")
f1.write(str(e))
else:
print("未发生错误")
finally:
print("最后一定会执行的操作")
?
?
?
?
?
import traceback
try:
f1 = open("D:/pythonStudy/day09/a.txt","a")
a = int(input("num1:"))//int(input("num2:"))
print(a)
except:
traceback.print_exc(file=f1)
?
class AgeError(Exception):
def __init__(self,errorInfo):
Exception.__init__(self)
self.errorInfo = errorInfo
def __str__(self):
return "年龄错误"
if __name__ =="__main__":
age = int(input("输入一个年龄"))
if age<1 or age>150:
raise AgeError(age)
else:
print(age)
?
|