Except with empty tuple

MRAB python at mrabarnett.plus.com
Wed Oct 16 13:18:52 EDT 2019


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?
> 
What if there's an exception?

 >>> def test_1():
...     try:
...         print('statement1')
...         raise ValueError()
...         print('statement2')
...     except ():
...         pass
...     print('statement3')
...
 >>> def test_2():
...     print('statement1')
...     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
 >>> test_2()
statement1
statement2
statement3

So, no, it's not equivalent.



More information about the Python-list mailing list