Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 675
ferry Avatar asked Dec 05 '15 05:12

ferry


People also ask

What is the syntax of try block?

Java try block is used to enclose the code that might throw an exception. It must be used within the method. If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.

What is try syntax in Python?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error.

What is a try block?

A try block is the block of code in which exceptions occur. A catch block catches and handles try block exceptions. The try/catch statement is used in many programming languages, including C programming language (C++ and C#), Java, JavaScript and Structured Query Language (SQL).

What is try in SQL?

A TRY... CATCH construct catches all execution errors that have a severity higher than 10 that do not close the database connection. A TRY block must be immediately followed by an associated CATCH block. Including any other statements between the END TRY and BEGIN CATCH statements generates a syntax error.


1 Answers

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.

like image 150
Dimitris Fasarakis Hilliard Avatar answered Oct 06 '22 04:10

Dimitris Fasarakis Hilliard