try:print('try')
r=10/0#除零错误print('result',r)except ZeroDivisionError as e:#假如上面没有#错误就执行该语句print('except',e)#获取错误提示finally:print('finally')#无论如何都会执行,同时释放内存
try
except division by zero
finally
try:print('try')
r=10/0except ZeroDivisionError as e:print('except',e)else:print('result',r)#计入try中没有错误就执行该语句finally:print('finally')
try
except division by zero
finally
raise ValueError('This is a ValueError !')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-26-dfda0edb5140> in <module>()
----> 1 raise ValueError('This is a ValueError !')
ValueError: This is a ValueError !
---------------------------------------------------------------------------
NotIntError Traceback (most recent call last)
<ipython-input-28-fd16324046e5> in <module>()
----> 1 raise NotIntError('This is not an Int value!')
NotIntError: This is not an Int value!
a =[1,2,'',4,5,'a',[1,2,3]]for ele in a:try:iftype(ele)!=int:#如果元素不是整形就抛出错误raise NotIntError('This is not an Int value!')except NotIntError as e:print(e.error)else:print(ele)
1
2
This is not an Int value!
4
5
This is not an Int value!
This is not an Int value!
import logging#导入错误日志包deffoo(num):try:return100/int(num)except Exception as e:
logging.exception(e)
foo(0)print('End')#即使出错也不会停止运行
ERROR:root:division by zero
Traceback (most recent call last):
File "<ipython-input-31-984915d67822>", line 5, in foo
return 100/int(num)
ZeroDivisionError: division by zero
End
import logging#导入错误日志包deffoo(num):try:return100/int(num)except Exception as e:
logging.exception(e)
foo(0)print('END')#即使出错也不会停止运行
ERROR:root:division by zero
Traceback (most recent call last):
File "<ipython-input-32-49dea5892d89>", line 5, in foo
return 100/int(num)
ZeroDivisionError: division by zero
END