點選檢視程式碼
while True:
try:
x = int(input("請輸入一個數字:"))
print("你輸入的數字是:", x)
except:
print("異常,輸入的不是數字:")
try:
a= input("請輸入被除數:")
b=input("請輸入除數:")
c=float(a)/float(b)
print(c)
except ZeroDivisionError:
print("異常:除數不能為零")
except TypeError:
print("異常:除數和被除數都應該為數值型別")
except BaseException as e:
print(e)
print(type(e))
class AgeError(Exception):
def __init__(self,errorInfo):
Exception.__init__(self)
self.errorinfo= errorInfo
def __str__(self):
return str(self.errorinfo)+",年齡錯誤!應該在1-150之間"
if __name__=="__main__":
age = int(input("輸入一個年齡:"))
if (age < 1 or age > 150):
raise AgeError(age)
else:
print("正常的年齡:", age)