On 10/16/2019 1:18 PM, MRAB wrote:
On 2019-10-16 15:03, Antoon Pardon wrote:
I would like to verify I understand correctly.

It is about the following construct:

     try:
         statement1
         statement2
         ...
     except ():
         pass

As far as I understand and my tests seem to confirm this, this
is equivallent to just

     statement1
     statement2
     ...

Am I correct or did I miss something?

Since except (): cannot catch anything, the two are externally equivalent given a particular behavior of the statements. Internally, the first takes a bit longer unless the irrelevant try-except is optimized away, but checking for this case would be a waste of time.

What if there's an exception?

Antoon is asking whether the two snippets are equivalent for any particular behavior of the sequence of statements.

 >>> def test_1():
...     try:
...         print('statement1')
...         raise ValueError()
...         print('statement2')
...     except ():
...         pass
...     print('statement3')
...
 >>> def test_2():
...     print('statement1')

If you add raise ValueError() to test with the same snippet behavior

...     print('statement2')
...     print('statement3')
...
 >>> test_1()
statement1
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 4, in test_1
ValueError

The point is that except (): cannot not catch any exception.

 >>> test_2()

Then you get the same output.

--
Terry Jan Reedy


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to