If you have an unique index on a table and try to to insert duplicate records in that table you get a Duplicate key violation error. You can handle this error more gracefully by capturing it. If you are using GORM in Golang, then you can easily check if an error is of type Duplicate Key.
Panics are unhandled errors in Golang which are not anticipated while writing code. In order to capture them we need to explicitly added recover statements so that our code can recover from these panics in a graceful way.