Page 1 of 1
Creating custom exceptions
Posted: Wed 12 May 2010 19:08
by alfrye
I am trying to create a custom exception class when I get a specific errors returned from the postgresql database. From my code I am calling a store procedure in postgres that is using the raise exception command when certain conditions are met. I would like to have a custom exception class in my app that will catch this specific error returned by the store procedure.
Posted: Mon 17 May 2010 15:37
by StanislavK
As I can understand, you need to perform the following steps:
1. Add raising a custom exception to the stored procedure.
2. Create a custom exception class, for example, CustomException.
3. Raise CustomException when catching PgSqlException with some custom properties, like in the following sample:
Code: Select all
try {
// Execute the procedure here.
}
catch (PgSqlException pgEx) {
string code = "Some custom code";
if (pgEx.ErrorCode == code)
throw new CustomException("Some custom message", pgEx);
}
Please tell us if some of these steps cause difficulties.