0

In python2, this is valid:

>>> try:
...     pass
... except Exception as (e, b):
...     pass

But in python3, I get a syntax error:

>>> try:
...     pass
... except Exception as (e, b):
  File "<stdin>", line 3
    except Exception as (e, b):
                        ^
SyntaxError: invalid syntax

How do I catch a tuple() exception in python 3?

Edit:

After some input, I did some more digging and I'll change the question:

I see this syntax in python2:

try:
    pass
except Exception, (num, msg):
    pass

And I have no idea what this does, but the syntax is valid in python2. This is a syntax error in python3, so I converted it to this:

try:
    pass
except Exception as (num, msg):
    pass

Which is still a syntax error. So, I guess the question is now, what was the original intent of the first syntax, and how is that done in python3 now?

David Mulder
  • 7,595
  • 11
  • 45
  • 61
  • 1
    I've never seen `except Exception as (e, b)` in the wild - even if it was allowed in Python 2.x, I'm not even sure what for - the except block is given a single object that's the exception object... Suspecting a wonky parsing thing you could do `except Exception, e` instead of `except Exception as e` in some versions of 2.x... but that got disallowed because it got confusing if people did `exception TypeError, ValueError, e` instead of `exception (TypeError, ValueError), e` etc... – Jon Clements Feb 07 '18 at 17:09
  • 1
    Perhaps they wanted to allow Exceptions that are iterable :P – Wombatz Feb 07 '18 at 17:12
  • Have a look at https://stackoverflow.com/questions/2535760/python-try-except-comma-vs-as-in-except and Martijn Pieters' comment on inspectorG4dget's answer. There's no such thing as a `tuple` exception... What is it you're trying to do here? – Jon Clements Feb 07 '18 at 17:21
  • I guess just made a dumb assumption about what it was doing (this is someone elses code). I'm not really sure what the original intent was then. – David Mulder Feb 07 '18 at 18:09
  • For frame of reference: https://github.com/samba-team/samba/blob/master/python/samba/tests/auth_log_pass_change.py On line 293. – David Mulder Feb 07 '18 at 18:16
  • Looking at this now, I wonder if this is *should have* been a syntax error, but is not in python2. – David Mulder Feb 07 '18 at 18:19
  • I might be mis-remembering - but exceptions way back when didn't have to be inherited from `BaseException` - you could just do `raise 'OOOPS!'` absolutely fine... or raise using `raise SomeException, 'oh no', 5` - which would raise `SomeException` with oh no and 5 as a tuple, so in the `except` - using `except ExceptionName, (arg1, arg2)` did actually work... the unified Exception system tidied that all up with explicit syntax and the requirements for a base class... – Jon Clements Feb 07 '18 at 18:49
  • Basically, until 3.x came along - because of the policy of not breaking backwards compatibility within the same major version - they just had to keep it even though the system changed and refined from 2.5 onwards towards what it is in 3.x now. (I seem to recall you couldn't even have an except *and a* finally in the same try block - not sure if that was fixed in 2.4/2.5 though) – Jon Clements Feb 07 '18 at 18:56

1 Answers1

1

In Python 2 that is valid syntax but a caught Exception is not a tuple so that will still fail if you actually encounter an Exception:

>>> try:
...     a
... except Exception as (e, b):
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ValueError: need more than 1 value to unpack

So what you want is just:

>>> try:
...     pass
... except ValueError as e:  # Note, a specific exception should be caught
...     pass
... 
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
  • I'm going to see if I can find it - but I'm guessing it was a side effect of being allowed to write `except Exception, e` instead of *having* to use `as`... – Jon Clements Feb 07 '18 at 17:11
  • @JonClements that sounds like a good guess, did you find anything? I’m on my phone and not been able to check – Chris_Rands Feb 07 '18 at 18:33
  • 1
    See comments on question - in short you couldn't even use `as` until 2.6 so the language syntax kind of evolved... – Jon Clements Feb 07 '18 at 18:37