Identifying error types

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an application where I need to classify the errors that occur when I'm
accessing the database but not changing its structure. There are a few types
of errors:
- Connectivity error: the database server cannot be accessed
- Query error: there is a parse error in the query I'm trying to execute
- Referential integrity error: a violation of referential integrity occurred

How can I filter the SqlException so that I could throw a more especific
exception?

Remarks: I don't wanna make a switch/case for the almost 4000 error numbers
of SQL Server.

Thanks
 
The SqlException class has an Errors collection. The thing to do is to trap
the SqlException and then

catch(SqlException ex){
foreach(SqlError er in ex.Errors){

}
}

Now based on your problem, the only way to throw your own error is to trap
these in your class and then throw your own derivced exception with the
information you want. Or, you can trhow your own exceptions in the stored
procedure and specify the numbers. For lower severity numbers you can trap
the InfoMessage of the SqlConnection and that will trap anything with a
severity of under 14 (it's around 14, I don't know the number off of the top
of my head but I'll post it in a second). This won't do anything for you
though if you connection doesn't open up for instance. And if you don't get
a connection to the db, I don't know of any way to get info from the server
since the connection hasn't been made and the server you're looking for may
not even exist.

HTH,

Bill
 
Back
Top