Trying to catch integrity error with SQLAlchemy <p>I'm having problems with trying to catch an error. I'm using Pyramid/SQLAlchemy and made a sign up form with email as the primary key. The problem is when a duplicate email is entered it raises a IntegrityError, so I'm trying to catch that error and provide a message but no matter what I do I can't catch it, the error keeps appearing.</p> <pre><code>try: new_user = Users(email, firstname, lastname, password) DBSession.add(new_user) return HTTPFound(location = request.route_url('new')) except IntegrityError: message1 = "Yikes! Your email already exists in our system. Did you forget your password?" </code></pre> <p>I get the same message when I tried <code>except exc.SQLAlchemyError</code> (although I want to catch specific errors and not a blanket catch all). I also tried <code>exc.IntegrityError</code> but no luck (although it exists in the API).</p> <p>Is there something wrong with my Python syntax, or is there something I need to do special in SQLAlchemy to catch it?</p> <hr> <p>I don't know how to solve this problem but I have a few ideas of what could be causing the problem. Maybe the try statement isn't failing but succeeding because SQLAlchemy is raising the exception itself and Pyramid is generating the view so the <code>except IntegrityError:</code> never gets activated. Or, more likely, I'm catching this error completely wrong.</p>