Python 异常

2019-11-28  本文已影响0人  tafanfly

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.

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 捕获多种异常

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

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

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.
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 ... 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

由此可知:

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.
上一篇 下一篇

猜你喜欢

热点阅读