Try statement syntax

I have been reading python documentation , could someone help me with interpreting this?

try_stmt  ::=  try1_stmt | try2_stmt
try1_stmt ::=  "try" ":" suite
               ("except" [expression [("as" | ",") identifier]] ":" suite)+
               ["else" ":" suite]
               ["finally" ":" suite]
try2_stmt ::=  "try" ":" suite
               "finally" ":" suite

I initially thought it meant that try statements had to have either formats

  • try And finally or
  • try , except , else AND finally .
  • But after reading the documentation, it mentioned that else is optional and so is finally . So, I was wondering what is the purpose of the documentation showing us the code in the above format to begin with?


    You do have two forms of the try statement. The main difference between them is that in the case of try1_stmt an except clause must be specified .

    In Introduction | Notation of the Python Language reference, the following is written:

    A star (*) means zero or more repetitions of the preceding item; likewise, a plus (+) means one or more repetitions , and a phrase enclosed in square brackets ([ ]) means zero or one occurrences (in other words, the enclosed phrase is optional) . The * and + operators bind as tightly as possible; parentheses are used for grouping .

    So, specifically, in the first form:

    try1_stmt ::=  "try" ":" suite
                   ("except" [expression [("as" | ",") identifier]] ":" suite)+
                   ["else" ":" suite]
                   ["finally" ":" suite]
    

    The else and finally clauses are optional ([]) , you only require a try statement and one or more (+) except clauses.

    In the second form:

    try2_stmt ::=  "try" ":" suite
                   "finally" ":" suite
    

    You only have a single try and a single finally clause with no except clauses.


    Do note that for the first case the order of the else and finally clauses are fixed. An else clause following a finally clause results in a SyntaxError .

    In the end of the day, this all boils down to essentially not being able to have a try clause together with only an else clause. So in code form these two are allowed:

    First form of the try statement ( try1_stmt ):

    try:
        x = 1/0
    except ZeroDivisionError:
        print("I'm mandatory in this form of try")
        print("Handling ZeroDivisionError")
    except NameError:
        print("I'm optional")
        print("Handling NameError")
    else:
        print("I'm optional")
        print("I execute if no exception occured")
    finally:
        print("I'm optional")
        print("I always execute no matter what")
    

    Second form ( try2_stmt ):

    try:
        x = 1/0
    finally:
        print("I'm mandatory in this form of try")
        print("I always execute no matter what")
    

    For an easy to read PEP on this subject, see PEP 341 which contains the original proposal for the two forms of the try statement.

    链接地址: http://www.djcxy.com/p/89206.html

    上一篇: 在CSS中拉伸背景图像

    下一篇: 尝试语句语法