前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 错误处理:try..exc

python 错误处理:try..exc

作者头像
py3study
发布2020-01-10 01:40:10
4090
发布2020-01-10 01:40:10
举报
文章被收录于专栏:python3python3

python错误继承表:

https://docs.python.org/3/library/exceptions.html#exception-hierarchy

格式:

def 函数():

      try:  

             内容        ###正确输出

      except 错误表  in e:

              输出内容 ###错误输出

       finally:  

               输出内容   ##必定输出

print('END')        ##必定输出

#!/usr/bin/python
# -*- coding: utf-8 -*-

def foo(s):
    return 10 / int(s)

def bar(s):
    return foo(s) * 2

def main():
    try:
        bar('0')
    except Exception as e:
        print('Error:', e)
    finally:
        print('finally...')

        
main()

运行结果:

('Error:', ZeroDivisionError('integer division or modulo by zero',))
finally...

a.面对函数层层调用,try...except能捕捉得到。

b.类的子类错误也能捕捉得到,如捕捉ValueError错误,顺便也会把UnicodeError也捕捉了

 +-- ValueError
      |    +-- UnicodeError
      |         +-- UnicodeDecodeError
      |         +-- UnicodeEncodeError
      |         +-- UnicodeTranslateError

记录错误到日志文件:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import logging ###########记得导入模块

def foo(s):
    return 10 / int(s)

def bar(s):
    return foo(s) * 2

def main():
    try:
        bar('0')
    except Exception as e:
        logging.exception(e) #########模块函数使用
        
print ('haha')        
main()
print ('END')

运行结果:

haha
END


ERROR:root:division by zero
Traceback (most recent call last):
  File "/usercode/file.py", line 14, in main
    bar('0')
  File "/usercode/file.py", line 10, in bar
    return foo(s) * 2
  File "/usercode/file.py", line 7, in foo
    return 10 / int(s)
ZeroDivisionError: division by zero

当不用错误调试时,普通的程序出错会调用栈Traceback提示错误

def foo(s):
    return 10 / int(s)

def bar(s):
    return foo(s) * 2

def main():
    bar('0')

main()

运行结果:

Traceback (most recent call last):
  File "/usercode/file.py", line 13, in <module>
    main()
  File "/usercode/file.py", line 11, in main
    bar('0')
  File "/usercode/file.py", line 8, in bar
    return foo(s) * 2
  File "/usercode/file.py", line 5, in foo
    return 10 / int(s)
ZeroDivisionError: integer division or modulo by zero

抛出错误:raise

def foo(s):
    n = int(s)
    if n==0:
        raise ValueError('invalid value: %s' % s)
    return 10 / n

def bar():
    try:
        foo('0')
    except ValueError as e:
        print('ValueError!', e)
        raise

bar()

运行结果:

('ValueError!', ValueError('invalid value: 0',))


Traceback (most recent call last):
  File "/usercode/file.py", line 17, in <module>
    bar()
  File "/usercode/file.py", line 12, in bar
    foo('0')
  File "/usercode/file.py", line 7, in foo
    raise ValueError('invalid value: %s' % s)
ValueError: invalid value: 0
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档