In [1]:
import math
for i in range(10):
try:
input_number=input('write a number')
if input_number=='q':
break
result=math.log(float(input_number))
print (result)
#异常捕捉
except ValueError:
print('ValueError:imput must>0')
#break
In [3]:
import math
for i in range(10):
try:
input_number=input('write a number')
if input_number=='q':
break
result=1 /math.log(float(input_number))
print (result)
#异常捕捉
except ZeroDivisionError:
print('log (value) must!=0')
#break
In [4]:
import math
for i in range(10):
try:
input_number=input('write a number')
if input_number=='q':
break
result=1 /math.log(float(input_number))
print (result)
#异常捕捉
except ValueError:
print('ValueError:imput must>0')
except ZeroDivisionError:
print('log (value) must!=0')
except Exception:
print('Unknow error')
In [5]:
class TangError(ValueError):
pass
cur_list=['tang','yu','di']
while True:
cur_input=input()
if cur_input not in cur_list:
raise TangError('Invaild input :%s' % cur_input)
In [6]:
try:
print('hello')
finally:
print('finally')#无论是否有异常都会执行
In [7]:
try:
1/0
except:
print('==0')
finally:
print('finally')#无论是否有异常都会执行