Parsing Exceptions

Posts   
 
    
nero2001
User
Posts: 8
Joined: 10-Oct-2006
# Posted on: 12-Jan-2007 18:39:47   

Hi there,

first i have to congratulate you all on your fine job supporting LLBLGEN wink

Now, my question: i have a "unique" constraint on my table, and when i try to insert something that violates it, llblgen raises an exception (ORMQueryExecutionException).

The problem is: this exception only gives me the information that something went wrong while executing a query, but it can be that unique constraint, or some other problem.

What is the best way to identify the problem? What i am planning to do is parse the exception message, and there i can search for certain words that will give me the indication of what went wrong..

Any suggestions?

Thanks in advance,

Nelson

Chester
Support Team
Posts: 223
Joined: 15-Jul-2005
# Posted on: 13-Jan-2007 23:37:28   

First, you should note that LLBLGen is wrapping the message from your database provider. The ORM Exception class will have it's own message but also report what the provider message was. So you can parse the ORM exception or check it's InnerException property to get at the original exception.

On one project my team created a class that scrubs exception messages by searching the string for certain things. e.g. If you got this message:

An exception was caught during the execution of an action query: Cannot insert duplicate key row in object 'dbo.Person' with unique index 'NK_Person'.\r\nThe statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

...you could search for "duplicate key row" and know that a unique constraint was violated, then tell the user something like "You either tried to create a new record and an identical one already exists, or you updated a record to have the same values as one that already exists." You get the idea.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 14-Jan-2007 09:38:37   

Another way is indeed to check the inner exception, and cast it to the native exception type (or re-throw it in a try/catch with catch clauses for the original type, e.g. SqlException). then, you can check the error code in THAT exception instance to determine what went wrong.

Frans Bouma | Lead developer LLBLGen Pro