none
How do I identity a specific (database) error? RRS feed

  • Question

  • WHAT I HAVE:

    Visual Basic 2010, .NET Framework 4.0, Entity Framework 4.0, SQL Express

    WHAT I NEED:

    How to get the specific nature of an exception, particularly SQL exceptions. "SQLException" doesn't tell me much about what went wrong and why. The Message string is useful for debugging and user feedback, but hard to work with in terms having the program figure out if and how to handle the error.  When I had classic VB there was a variety of numeric codes, but with Try blocks I should be able to use "xxxxxException" identifiers (say, FileNotFoundException, AccessViolationException, InvalidArgumentException--and perhaps some other more specialized, data-related identifiers). Unfortunately, there is no list of specific exceptions, let alone specific data exceptions, in the help. (If there is, I don't know where to look.) I'd like it if there was some way (other than parsing Exception.Message) to pin down the precise nature of the error so I can handle specific types of data errors. At the very least I'd like to know how to programmatically obtain specific info on common data errors.

    rough idea (the fine details might be wrong) of what I'm trying to do is below:

    Try

       '   do some data stuff

     Catch ex As SqlException

       If TypeOf ex Is xxxxxException Then

          '   one kind of (data) error: do this

        ElseIf TypeOf ex Is yyyyyException Then

          '   another kind of error: do that

        ...

        Else

          Throw 'other error

       End If

     Finally

       '   clean up

    End Try

      


    Robert Gustafson







    Friday, October 11, 2013 1:59 AM

Answers

  • <copied>

    So, no matter what, I have to parse the .Message property? That's easier for the programmer than for the program itself! It sounds like I need to write a whole parsing routine just to get at the root of any data-based error. 

    <end>

    I don't know what you are talking about here.  Parsing the Message Property?

    I gave you two things.

    1) How to query SQL Server's Master database for the list of error codes and their descriptions the entire shooting match, which you can printout and tape the list to a wall if nothing else.

    2) How to do a try/catch on a SQLException and within the SQLException  how to look at the returned error code to determine what is the specific error.

    Actually, you are trying to reinvent the wheel with this parsing thing.

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlerror.aspx

    Monday, October 14, 2013 12:12 AM

All replies

  • Hello,

    From your description, if I understand correctly, you want to know how to designing custom exceptions.

    For this, there is links regarding it:

    Designing Custom Exceptions:

    http://msdn.microsoft.com/en-us/library/vstudio/ms229064(v=vs.100).aspx

    How to: Create New Exception Classes in Visual Basic

    http://msdn.microsoft.com/en-us/library/vstudio/6sh1zxsd(v=vs.100).aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 11, 2013 9:48 AM
    Moderator
  • Go to SQL Server Studio Manager and run the query

    USE MASTER
    GO
    SELECT * FROM SYS.MESSAGES

    Look at message-id 113 and its message in the results of the query and compare it to the list.

    http://www.sql-server-helper.com/error-messages/msg-1-500.aspx

    Try

    Catch ex As SqlException

    if ex.Errorcode = 113 then

    do something

    end if

    Catch ex as DivideByZeroException

    blah blah

    '' It's none of those exceptions so just catch the exception using the generic catch catching any exception.

    Catch ex as Exception

    blah blah

    end try

    You can do the same thing in VB with multiple catches in a try/catch

    http://www.codeproject.com/Articles/154121/Using-Try-Catch-Finally

    Friday, October 11, 2013 11:07 AM
  • I'm not necessarily looking to create custom exceptions. I'm looking to obtain--for error-handling purposes (as opposed to error-debugging or error-displaying purposes)--more specific information about an error. If, say, an SqlException occurs, I want the program to be able to determine the how, where, and why--and from that, how best to handle the error.

    I've seen a number of Exception-class properties that might possibly fit the bill--.HResult, .ErrCode, etc. What about them? Also, are there any specific data-related exceptions that, say, inherit from SqlException or some other kind of exception? Are there any exceptions besides "SqlException" (per se) that a database program might trigger? What kinds of exceptions are common when working with data, and how do I determine which one I'm dealing with in a given situation?

    PS. I don't like to, except in a cursory manner, parse message strings toward that end because computer code, as opposed to a human brain, is lousy at intellegently interpreting words. I'm also iffy about the below suggestion of running a master-database query in order to determine the cause of an error. (A query to find out why another query failed?) And it might raise an exception of its own. (An error resulting from examining an error? Not too elegant.)

    PPS. My general philosophy is that a program should try to avoid crashing in response to an exception whenever possible. Even more important, if a programmer can anticipate certain disproportionately-likely kinds of (data-related) errors, given an app's particular nature, then the program should be able to recognize those errors when they arise and, when possible, deal with them intelligently.


    Robert Gustafson






    Sunday, October 13, 2013 10:08 AM
  • Need to add more information during your business. You should add class name, method, basic parameters. This allow you to regenerate error. For other errors like connections, parameters, It will appear in SqlException and Exception message.


    Tarek Yehia

    Sunday, October 13, 2013 11:11 AM
  • So, no matter what, I have to parse the .Message property? That's easier for the programmer than for the program itself! It sounds like I need to write a whole parsing routine just to get at the root of any data-based error. 

    Robert Gustafson

    Sunday, October 13, 2013 4:02 PM
  • <copied>

    So, no matter what, I have to parse the .Message property? That's easier for the programmer than for the program itself! It sounds like I need to write a whole parsing routine just to get at the root of any data-based error. 

    <end>

    I don't know what you are talking about here.  Parsing the Message Property?

    I gave you two things.

    1) How to query SQL Server's Master database for the list of error codes and their descriptions the entire shooting match, which you can printout and tape the list to a wall if nothing else.

    2) How to do a try/catch on a SQLException and within the SQLException  how to look at the returned error code to determine what is the specific error.

    Actually, you are trying to reinvent the wheel with this parsing thing.

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlerror.aspx

    Monday, October 14, 2013 12:12 AM
  • Exactly what I thought! I figured there had to be an easier way than parsing error-message text, and it seems like the link to the members of the SqlException class--particularly properties like .Number and .Procedure--should do the trick. (If you ask the right question enough ways, the answer will present itself.)

    Thank you!


    Robert Gustafson


    Monday, October 14, 2013 1:14 AM