ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python学习笔记:异常处理

2021-03-05 17:35:33  阅读:234  来源: 互联网

标签:Python except here ZeroDivisionError 笔记 error print 异常


Python学习笔记:异常处理

基本的异常处理

python与其它语言一样,也是支持异常处理的,我们看一个例子:

n=1/0

输出

File "d:\workspace\python\set.py", line 1, in
n=1/0
ZeroDivisionError: division by zero

可以看到,除零错误以一个ZeroDivisionError形式抛出。

这里和其它语言中一样,可以用try/catch语句来捕获异常,稍有区别的是这里写作try/except,但效果相同:

try:
    n=1/0
except:
    print("here is a catched error")

输出

here is a catched error

这里没有指定捕获的异常类型,类似在PHP中指定基类Exception,可以捕获到所有类型,当然我们也可以捕获指定异常:

try:
    n = 1/0
except ZeroDivisionError:
    print("here is a catched ZeroDivisionError")

输出

here is a catched ZeroDivisionError

如果需要捕获多种异常,然后分情况处理的,这样写:

try:
    n = 1/0
except ZeroDivisionError:
    print("here is a catched ZeroDivisionError")
except Exception:
    print("here is a catched Exception")

输出

here is a catched ZeroDivisionError

此外python同样支持finaly:

def testTryCatch(a):
    try:
        n = 1/a
    except ZeroDivisionError:
        print("here is a catched ZeroDivisionError")
    except Exception:
        print("here is a catched Exception")
    finally:
        print("whatever error will happen here is will called")
testTryCatch(0)
testTryCatch(1)

输出

here is a catched ZeroDivisionError
whatever error will happen here is will called
whatever error will happen here is will called

无论是否捕获到异常,finally后的语句都会输出。

异常继承关系

python的异常继承关系与PHP略有不同,下面简单罗列一些python的继承关系:

  • BaseException
    • SystemExit
    • KeyboardInterrupt
    • GeneratorExit
    • Exception
      • ArithmeticError
        • ZeroDivisionError

可以看出在Python中,最基础的异常类是BaseException,它是所有异常的基类,可以用于捕获各种类型的异常。

更多的异常继承关系请阅读简明教程

抛出异常

当然,Python同样支持抛出异常,但并不是throw,而是使用raise

对于Python并未采用其它语言的一些关键字我也是颇为费解,但对我等码农来说,学就完事了。

def testTryCatch(a):
    try:
        n = 1/a
    except ZeroDivisionError as error:
        print("I don't want deal ZeroDivisionError")
        raise error


try:
    testTryCatch(0)
except BaseException as error:
    print(type(error))

输出

I don't want deal ZeroDivisionError
<class 'ZeroDivisionError'>

在上边的例子中,testTryCatch程序中捕获了异常,但没有处理,直接抛出,到外部主程序中才进一步处理。此外可以看到,我们用句柄BaseException来捕获异常,但异常类型并未改变。

自定义异常

除了处理程序运行当中编译器抛出的异常,开发者有时候也会主动定义一些异常并抛出,而且这相当有用,尤其是在编写一些网络服务时,用异常来实现错误处理会让程序相当简洁。

class customError(Exception):
    def __init__(self):
        pass
    def __str__(self):
        return "this is a custom error"

try:
    error=customError()
    raise error
except Exception as e:
    print(str(e))

输出

this is a custom error

上面的例子中,定义了一个用户自定义异常customError,可以看到和类定义完全没什么两样,并且在异常中重写了__str__便于输出自定义信息,如果在网络开发中,可以进一步定义相应的错误码和错误提示信息以方便客户端接收。

日志

除了异常,Python还提供一个用于错误排查的模块——日志:

import logging
logging.basicConfig(filename='main.log',
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(message)s')
logging.error("this is error message1")
logging.error("this is error message2")

main.log

2021-03-05 17:16:34,829 - set.py[line:4] - this is error message1

2021-03-05 17:16:34,831 - set.py[line:5] - this is error message2

使用logging.basicConfig()对logging模块设置后,就可以很容易地记录程序错误信息了,至少比用户自行定义日志记录功能要方便很多。

调试

简明教程介绍了一种Python的手工调试工具Pdp,但老实说,VSCode的调试功能已经相当强大了,所以这里不想多做赘述,想了解Pdp调试的请移步简明教程

标签:Python,except,here,ZeroDivisionError,笔记,error,print,异常
来源: https://www.cnblogs.com/Moon-Face/p/14487424.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有