zoukankan      html  css  js  c++  java
  • Python学习笔记:异常处理

    Python学习笔记:异常处理

    基本的异常处理

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

    n=1/0
    

    输出

    File "d:workspacepythonset.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调试的请移步简明教程

    本篇文章首发自魔芋红茶的博客https://www.cnblogs.com/Moon-Face/ 请尊重其他人的劳动成功,转载请注明。
  • 相关阅读:
    直接选择排序(C++模版技术实现)
    求素数
    快速排序(C++模版技术实现)
    堆排序(C++模版技术实现)
    简单链式二叉树(C++模版技术实现)
    归并排序(C++模版技术实现)
    求斐波那契数列的两种解法
    C++中改变setw(n)的对齐方式
    C中的64位整型
    Windows版GCC之TDMGCC 4.5.2
  • 原文地址:https://www.cnblogs.com/Moon-Face/p/14487424.html
Copyright © 2011-2022 走看看