Python, exception
Like Java, Pyton exception objects indicate exceptions. When an error occurs, an exception is thrown. If the exception object is not processed or captured, the program uses the so-called trace ack to terminate the execution;
>>> 1/0
Traceback (most recent call last ):
File" ", Line 1, in
ZeroDivisionError: Division by zero
The program can manually throw an exception through raise javastin.
>>>Raise Exception
Traceback (most recent call last ):
File" ", Line 1, in
Exception
>>>Raise Exception ("hello exception ")
Traceback (most recent call last ):
File" ", Line 1, in
Exception: hello exception
To customize the Exception type, you only need to inherit the Exception (or its subclass;
>>>Raise 1
Traceback (most recent call last ):
File" ", Line 1, in
TypeError:Exceptions must derive from BaseException
Unlike C ++, only exception classes can be thrown;
>>> Class MyException (Exception):
... Pass
...
>>>Raise MyException
Traceback (most recent call last ):
File" ", Line 1, in
_ Main _. MyException
Capture exceptions
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + dhj5o1_vcd4kpha + signature + OjwvcD4KPHA + oa08L3A + signature + ICA8L3A + signature + z + signature/Signature/ ojrL/logs + Pj4 + IHRyeTo8L3A + signature + ZWxzZbXEv + nU2sO709DS/Signature + Pj4 + signature + OjwvcD4KPHA + signature + Pj4 + IHRyeTo8L3A + CjxwPi4uLiAxLzA8L3A + signature "finally ')
...
Finally
Traceback (most recent call last ):
File" ", Line 2, in
ZeroDivisionError: division by zero
>>> Def f ():
... Try:
.... Return 1/0
... Finally:
... Return 2
...
>>> F ()
2