I want to throw a custom exception when my Insert fails, which violates PK / FK relationship.
Now, LINQ TO SQL throws SQLException, I want to throw a more specific exception like CategoryException.
I want to throw a custom exception when my Insert fails, which violates PK
Share
When you catch the
SQLExceptionyou can check itsNumberproperty or navigate to itsErrorsproperty and check if it contains anySqlErrorwith number 547 – that should be constraints violation. If you need details about what constraint was violated and on which table you must really parse the message as @tyrongower mentioned. The pattern for the message is:Edit:
Depending on the type of application you are developing you should be aware of localization. SQL server can localize its error messages so if you deploy the application to the server using different locale setting your exception message parsing will not work.