python exception handling

版权声明:本文全部是胡说八道,如果你喜欢,可随意转载 https://blog.csdn.net/robinsongsog/article/details/79904509

try/except form:

   the complete form of try/except/else/finally looks like the following:

try:
    Normal execution block
    except A:
        Exception A handle
    except B:
        Exception B handle
    except:
        Other exception handle
    else:
        if no exception, get here
    finally:
        print("finally")

in the normal condition, Normal execution block will be executed, if exception occurred, then interrupt the current execution, transfer the control to the exception handler, python will match the handler from the first except X, whatever exception occurred, if we have provide the finally statement,.  after the execution of try/except/else, finally statements will be executed.


please attention:

1. the correct sequence must be:

try-->except X -->except-->else-->finally,

that is to say, all the exception handler must executed before else and finally, and else should before finally, or it will generate syntax error.

2. both else and finally are optional,  finally must existing in the end of whole statement.

3. in the above example,  else statement must cooperate with except X or except,if else isolated used without except,

it will generate syntax error, that is to say,  else cannot cooperate with try/finally .


猜你喜欢

转载自blog.csdn.net/robinsongsog/article/details/79904509
今日推荐