It depends on whether having a duplicate is an exceptional scenario, or a business-logic case.
For example, checking for unique email/username during registration is a business-logic case and the check should be done before trying to insert
If you are required to indicate which field exactly has failed with the unique constraint, you'd better check it beforehand, rather than catching the exception. Catching the exception doesn't give you the important detail - which field has failed.
There are ways to obtain this information, based on the exception, but it is very tedious and is database-specific (lookup the constraint name in the DB (db-specific), get the field it is applied on, match the field with the entity property)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…