try except except finally

import traceback
import time


class A(object):
    def __init__(self):
        print(f'Initializing {self.__class__.__name__}')
    def __del__(self):
        print(f'Releasing {self.__class__.__name__}')
        
class B(A):
    pass

def tt(func, li):
    b = B()
    print('run tt  111 ...')
    for n in li:
        print(func(n))
    print('run tt  222 ...')
    return 'CCCCCC'

def test(n):
    a = A()
    try:
        x = 100/n
        print(n, x)
        time.sleep(2)
    except Exception:
        print('Exception')
        print(traceback.format_exc())
        print('Exception go ...')
        return 'abnormal end from Exception in test...'
        # 因为test是一个函数, 函数一定会有返回值, 这个默认有 return None
    except KeyboardInterrupt: # 如果except捕获了KeyboardInterrupt异常,那么执行except KeyboardInterrupt:语句块,当执行到exit(2)语句时,解释器会把finally:语句块先执行完,然后释放资源, 然后退出程序前,打印Process finished with exit code 1到标准输出,      print('exit 2')语句和print('End...')是执行不到了
        print('KeyboardInterrupt')
        print(traceback.format_exc())
        print('KeyboardInterrupt go ...')
        exit(2)
        print('exit 2')
        # 因为test是一个函数, 函数一定会有返回值, 这个默认有 return None
    finally:
        print('finally')
    print('normal end before return...')
    return 'normal end from test...'  # 如果没有捕获到任何异常,那么函数执行到return 语句时,会先释放函数内的变量标识符(如果引用对象计数是1,那么解释器垃圾回收机制会释放标识符引用的对象),然后return 返回.
    
if __name__ == '__main__':
    li = [1, 2, 0, 3, 0, 5]
    print(tt(test, li))
    print('End...')
    

Initializing B
run tt 111 ...
Initializing A
1 100.0
finally
normal end before return...
Releasing A
normal end from test...
Initializing A
2 50.0
finally
normal end before return...
Releasing A
normal end from test...
Initializing A
Exception
Traceback (most recent call last):
File "C:\Git_pro\ttttttttttttttttttttttttt\test.py", line 25, in test
x = 100/n
ZeroDivisionError: division by zero

Exception go ...
finally
Releasing A
abnormal end from Exception in test...
Initializing A
3 33.333333333333336
finally
normal end before return...
Releasing A
normal end from test...
Initializing A
Exception
Traceback (most recent call last):
File "C:\Git_pro\ttttttttttttttttttttttttt\test.py", line 25, in test
x = 100/n
ZeroDivisionError: division by zero

Exception go ...
finally
Releasing A
abnormal end from Exception in test...
Initializing A
5 20.0
finally
normal end before return...
Releasing A
normal end from test...
run tt 222 ...
Releasing B
CCCCCC
End...

Process finished with exit code 0

 

----------------------------------在迭代列表中的第二元素后,在time.sleep(2)中, 执行Ctrl + C, 触发KeyboardInterrupt异常 如下:

Initializing B
run tt 111 ...
Initializing A
1 100.0
finally
normal end before return...
Releasing A
normal end from test...
Initializing A
2 50.0
KeyboardInterrupt
Traceback (most recent call last):
File "C:\Git_pro\Clone\cloudperformancetest\XGS\ooklatool\test.py", line 27, in test
time.sleep(2)
KeyboardInterrupt

KeyboardInterrupt go ...
finally
Releasing A
Releasing B

Process finished with exit code 2

 

posted @ 2023-12-28 19:43  fangpinz  阅读(2)  评论(0编辑  收藏  举报