简体   繁体   中英

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

  1. try And finally or
  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM