Creating custom exceptions

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
Posts: 16
Joined: Wed 05 May 2010 19:39

Creating custom exceptions

Post by alfrye » Wed 12 May 2010 19:08

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

Post by StanislavK » Mon 17 May 2010 15:37

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.

Post Reply