How to catch specific exception without error number?

Posted by CJ7 on Stack Overflow See other posts from Stack Overflow or by CJ7
Published on 2012-10-09T09:31:07Z Indexed on 2012/10/09 9:37 UTC
Read the original article Hit count: 274

I need to catch the following specific exception:

System.Data.OleDb.OleDbException was caught
ErrorCode=-2147467259
Message="The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."
Source="Microsoft JET Database Engine"

I'm not sure what ErrorCode is but it looks unreliable. Can I rely on Message being identical across platforms? Is the only solution to do a text search of Message for words like duplicate and primary key?

Note: see my question here for why I need to catch this exception.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about exception-handling