美文网首页
Python 异常

Python 异常

作者: tafanfly | 来源:发表于2019-11-28 17:04 被阅读0次

Python 含有异常处理机制来帮助用户处理可能发生的错误异常。

1. 异常概念

异常是指Python程序运行过程中遇到的错误, 每个异常都是某个异常类的实例。下面实例中未对异常做任何处理,导致程序终止并且抛出错误信息。其中TypeError是异常类中的一种。

def test(a=1, b='2'):
    return a + b

test()

#result
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    test()
  File "test.py", line 2, in test
    return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'

2. 抛出异常

Python中抛出异常必须用到raise语句, 后面接上异常类(会自动创建实例)或者异常类的实例

raise Exception
#result
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    raise Exception
Exception
#############################
raise Exception()

#result
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    raise Exception
Exception
#############################
raise Exception('Meet Error.')

#result
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    raise Exception('Meet Error.')
Exception: Meet Error.

抛出异常的进阶:

def test(a=1, b='2'):
    return a + b

try:
    test()
except Exception as e:
    raise
    #raise RuntimeError('Raise Error.')
    #raise RuntimeError('Raise Error.') from e
    #raise RuntimeError('Raise Error.') from None

#result 1
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    test()
  File "test.py", line 2, in test
    return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'

#result 2
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    test()
  File "test.py", line 2, in test
    return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    raise RuntimeError('Raise Error.')
RuntimeError: Raise Error.

#result 3
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    test()
  File "test.py", line 2, in test
    return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    raise RuntimeError('Raise Error.') from e
RuntimeError: Raise Error.

#result 4
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    raise RuntimeError('Raise Error.') from None
RuntimeError: Raise Error.
  • 第一种: 直接raise, 捕获异常后,重新引发该异常
  • 第二种: 抛出RuntimeError,处理异常时发生了新的异常,更倾向于新异常与正在处理的异常没有关系
  • 第三种: from e, 指出新异常是因旧异常直接引起的, 有助于后续对异常的分析和排查
  • 第四种: from None, 明确禁止异常关联

3. 异常类

异常类分两种, 一是内置异常类,如ExceptionOSErrorSyntaxError等, 直接拿来就可以用。
BaseException是所有异常类的基类。详见内置异常

image.png

二是为满足个人需求的自定义异常类, 这种类必须要直接或间接地继承 Exception, 然后根据需求是否添加方法。

class CaseError(Exception):
   pass

raise CaseError('Case is error.')

#result
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    raise CaseError('Case is error.')
__main__.CaseError: Case is error.

4. 捕获异常

可以使用try/except来处理可能发生异常的语句。

4.1 捕获一种异常

使用try/except + 异常类名来捕获该异常

def test(a=1, b='2'):
    return a + b

try:
    test()
except TypeError:
    print ('Meet type error.')

#result
Meet type error.
4.2 捕获多种异常

有时候程序往往出现不止一种异常, 那如何捕获多个异常呢,有下面的两种方法。

  • 多层次捕获: 层次分明, 针对不同的异常做不同的处理
    注意:后面第二个至第n个类中不能是第一个的子类, 否则该异常永远都无法被捕获
def test(a=1, b='2'):
    return a + b

try:
    test()
except TypeError:
    print ('It is type error.')
except NameError:
    print ('It is name error.')

#result
It is type error.
  • 一起捕获: 把所有的异常类放在一个元祖里面
def test(a=1, b='2'):
    return a + b

try:
    test()
except (TypeError,  NameError):
    print ('Meet error.')

#result
Meet error.
4.3 捕获所有异常

捕获所有异常有两种常见的格式, 分别是try/excepttry/except Exception

  • try/except 会捕获所有异常,包含键盘中断(KeyboardInterrupt)程序退出请求(SystemExit),慎用之。可能会使sys.exit() 语句被捕获,导致无法退出脚本。
import sys

try:
    sys.exit(0)
    print ('Exit the function.')
except:
    print ('Meet error.')

#result
Meet error.
# sleep 的时候,按Ctrl+c 
import time

try:
    time.sleep(20)
    print ('Sleep 20s.')
except:
    print ('Meet error.')

#result,^C表示Ctrl+c 
^CMeet Error.
  • try/except Exception 会捕获大部分异常,除了键盘中断(KeyboardInterrupt)程序退出请求(SystemExit),因为它们是从 BaseException ( Exception 的超类)派生而来的。
import sys

try:
    sys.exit(0)
    print ('Exit the function.')
except Exception:
    print ('Meet error.')
#result
退出脚本

###############
import time

try:
    time.sleep(20)
    print ('Sleep 20s.')
except Exception:
    print ('Meet error.')

#result
^CTraceback (most recent call last):
  File "test.py", line 4, in <module>
    time.sleep(20)
KeyboardInterrupt

5. 获取更多的异常信息

当捕获异常的时候, 我们需要获取更多的信息去定位和解决问题。
这里有三种方式。一是try/except XXX as e, 另外是traceback内置模块,第三是logging内置模块。

1. e表示异常对象
2. print_exc():对异常的输出
3. format_exc():把异常以字符串的形式返回,print(traceback.format_exc())等同于于traceback.print_exc()
4. print_exception():traceback.print_exc()实现方式就是traceback.print_exception(sys.exc_info())
import sys
import logging
import traceback

def test(a=1, b='2'):
    return a + b

try:
    test()
except Exception as e:
    print ('****** print object e ******')
    print ('Exception type is %s.' % type(e))
    print ('e is %s.' % e)

    print ('****** traceback.print_exc() ******')
    traceback.print_exc()

    print ('****** traceback.format_exc() ******')
    print(traceback.format_exc())

    print ('****** traceback.print_exception() ******')
    traceback.print_exception(*sys.exc_info())

    print ('****** logging_exception() ******')
    traceback.print_exception(*sys.exc_info())
###########result###########
****** print object e ******
Exception type is <class 'TypeError'>.
e is unsupported operand type(s) for +: 'int' and 'str'.
****** traceback.print_exc() ******
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    test()
  File "test.py", line 5, in test
    return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
****** traceback.format_exc() ******
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    test()
  File "test.py", line 5, in test
    return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'

****** traceback.print_exception() ******
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    test()
  File "test.py", line 5, in test
    return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
****** logging_exception() ******
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    test()
  File "test.py", line 6, in test
    return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'

6. else 和 finally

try/except ... else举例: else 在主 try 块没有引发异常时执行

def except_test():
    try:
        print ('Running')
        raise Exception('exception raised')
        return 0
    except Exception:
        print('Here have exception')
        return 1
    else:
        print('No exception')
        return 2

print (except_test())

#如果没有发生异常, 则返回
Running
0
#如果没有发生异常,且注释掉return 0, 则返回
Running
No exception
2
#如果发生异常,则返回
Running
Here have exception
1
#如果发生异常,且注释掉return 1, 则返回
Running
Here have exception
None

由此可见:

  • try 中有异常,会直接跳到except语句
  • try 中没有异常, 且没有return,才会跳到else语句

try/except ... else ... finally举例:无论是否引发异常都将执行

def except_test():
    try:
        print ('Running')
        raise Exception('exception raised')
        return 0
    except Exception:
        print('Here have exception')
        return 1
    else:
        print('No exception')
        return 2
    finally:
        print ('Do clean work')
        return 3

print (except_test())

#如果没有发生异常, 则返回
Running
Do clean work
3
#如果没有发生异常,且注释掉return 0, 则返回
Running
No exception
Do clean work
3
#如果没有发生异常,且注释掉return 3, 则返回
Running
Do clean work
0
#如果没有发生异常,且注释掉return 0 和 return 3, 则返回
Running
No exception
Do clean work
2
#如果发生异常, 则返回
Running
Here have exception
Do clean work
3
#如果发生异常, 且注释掉return 3,则返回
Running
Here have exception
Do clean work
1

由此可知:

  • try 中不管有没有异常, 有没有return, 最终都会运行finally, 且如果finally 中有return,则函数返回该return
  • finally return 优先级》try/except return 优先级》else return

7. 异常是向上传播的

在函数中引发异常时,异常将传播到调用函数的地方。

def err():
    raise Exception('Error.')

def test_err():
    err()

def test_err_too():
    test_err()

test_err_too()

#result
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    test_err_too()
  File "test.py", line 8, in test_err_too
    test_err()
  File "test.py", line 5, in test_err
    err()
  File "test.py", line 2, in err
    raise Exception('Error.')
Exception: Error.

相关文章

  • 24:python中的异常

    异常: 首先看看python的异常继承树 python的异常分为两种. 1、内建异常:就是python自己定义的异...

  • Python异常处理

    参考 Python菜鸟教程错误与异常 Python 异常处理 错误和异常 Python中(至少)有两种错误:语法错...

  • Python学习(八)

    异常处理 Python中的异常类型总结: Python内置异常类的层次结构: 异常检测 try-except语句 ...

  • (三)python错误与异常&面向对象编程

    python错误与异常 语法错误与定位 异常捕获、异常处理 try:xxxexcept: 自定义异常 python...

  • Python错误处理机制

    @(python程序员)[Python] Python Cookbook 捕获所有的异常 处理异常的时候最好还会尽...

  • Python异常处理

    Python中文件的操作 Python异常的处理 异常的引发 try⋯finally的使用

  • Python常见异常

    python标准异常 异常名称 描述 BaseException 所有异常的基...

  • Python 异常

    Python 含有异常处理机制来帮助用户处理可能发生的错误异常。 1. 异常概念 异常是指Python程序运行过程...

  • 异常处理

    捕获异常 raise语法: 格式:raise 异常名称(‘异常描述’) python中常见的异常

  • python的异常处理

    Python异常处理

网友评论

      本文标题:Python 异常

      本文链接:https://www.haomeiwen.com/subject/sjmzictx.html