Creating custom exceptions
Creating custom exceptions
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.
-
- Devart Team
- Posts: 1710
- Joined: Thu 03 Dec 2009 10:48
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:
Please tell us if some of these steps cause difficulties.
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);
}