pythonpython-3.xexcept

Invalid syntax (SyntaxError) in except handler when using comma


I have this code:

@app.route('/login/', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        session['username'] = request.form['username']
        session['password'] = request.form['password']
        try:
            # use reddit_api's login
            r.login(user=session['username'], password=session['password'])
        except InvalidUserPass, e:
            error = 'Incorrect username or password. '
        if not error:
            subreddits = r.user.get_my_reddits(limit=25)
            my_reddits = []
            for i in range(25):
                my_reddits.append(subreddits.next().display_name)
            session['my_reddits'] = my_reddits
            return redirect(url_for('index'))
    return render_template('login.html', error=error)

In 2.x, it worked fine, but in 3.x I get an error message like:

  File "app.py", line 101
    except InvalidUserPass, e:
                          ^
SyntaxError: invalid syntax

Why does this occur, and how can I fix it?


Solution

  • Change

    except InvalidUserPass, e:
    

    to

    except InvalidUserPass as e:
    

    See this for more info.