-10

I'm using a code in python 3 that was written in python 2 syntax.
The code have the right libraries and everything is working fine but this gives me an error:

        except Exception, _hx_e:
        _hx_e1 = _hx_e.val if isinstance(_hx_e, _HxException) else _hx_e
        e = _hx_e1
        tmp = None

Is there a way to use this , python 2 annotation instead of as in python 3?

AsafH
  • 89
  • 2
  • 9
  • 2
    Why not use the right syntax? You should also attempt to switch your python2 to python3, as python2's end of life is weeks away https://www.python.org/doc/sunset-python-2/ – Richard Stoeffel Dec 17 '19 at 15:29
  • 2
    Does this answer your question? [Python try...except comma vs 'as' in except](https://stackoverflow.com/questions/2535760/python-try-except-comma-vs-as-in-except) – Sayse Dec 17 '19 at 15:33
  • 1
    Answer: no, you can't. – bruno desthuilliers Dec 17 '19 at 15:37
  • @RichardStoeffel I'm using a library for school project that they made... don't ask me why they are doing this... I guess that they made this library in the past and didn't updated it – AsafH Dec 17 '19 at 15:39
  • :( @brunodesthuilliers – AsafH Dec 17 '19 at 15:52
  • @AsafH I'm currently porting a very complex and quite huge codebase to Python3 so I can feel your pain. But facts are facts and your only solution here is to to port this code to Python3 (assuming you need it to run on python3 of course, but py2 is almost dead, really). – bruno desthuilliers Dec 17 '19 at 15:57

1 Answers1

1

The old syntax is no longer valid.

Source

In Python 2, the syntax for catching exceptions was except ExceptionType:, or except ExceptionType, target: when the exception object is desired. ExceptionType can be a tuple, as in, for example, except (TypeError, ValueError):.

This could result in hard-to-spot bugs: the command except TypeError, ValueError: (note lack of parentheses) will only handle TypeError. It will also assign the exception object to the name ValueError, shadowing the built-in.

To fix this, Python 2.6 introduced an alternate syntax: except ExceptionType as target:. In Python 3, the old syntax is no longer allowed.

You will need to switch to the new syntax. The recommended fixer works quite reliably, and it also fixes the Iterating Exceptions problem described below.

Amiram
  • 1,227
  • 6
  • 14