It depends on whether the duplicate is an exceptional scenario or a business logic case.
For example, checking a unique email / username during registration is a business logical case, and verification must be completed before trying to insert
If you need to specify which field was executed with a unique constraint, it is better to check it beforehand rather than catch the exception. Catching an exception does not give you important information - in which field it was not executed.
There are ways to get this information based on an exception, but it is very tedious and specific to the database (find the name of the constraint in the database (taking into account the specific database), get the field to which it is applied, correspond to the field with the entity property)
Bozho source share