Benny Ae
Benny Ae

Reputation: 2026

Catch DB error details

I want a better way to catch database error details. I'm currently using :

try
{

    dbconn.table.AddObject(newRow);
    dbconn.SaveChanges();

}
catch (Exception ex)
{
    Console.WriteLine("DB fail ID:" + Row.id);
}

many times I found the Exception ex can no give me details on how the exception happen.

I think these exception most likely to be the DB connection kind.

So is there a better way to catch this ?

Upvotes: 1

Views: 1353

Answers (4)

Brian
Brian

Reputation: 5119

In short, yes there is a better way to handle it. The 'how' of it is up to you.

Exception handling in C# goes from the most specific exception type to the least specific. Also, you aren't limited to using just one catch block. You can have many of them.

As an example:

try
{
   // Perform some actions here. 
}

catch (Exception exc) // This is the most generic exception type.
{
   // Handle your exception here.
}

The above code is what you already have. To show an example of what you may want:

try
{
   // Perform some actions here. 
}

catch (SqlException sqlExc) // This is a more specific exception type.
{
   // Handle your exception here.
}

catch (Exception exc) // This is the most generic exception type.
{
   // Handle your exception here.
}

In Visual Studio, it is possible to see a list of (most) exceptions by pressing CTRL+ALT+E.

Upvotes: 0

MRB
MRB

Reputation: 3822

Instead of Exception use SqlException. SqlException give you more detail. it has a Number property that indicate type of error and you can use that Number in a switch case to give some related information to user.

Upvotes: 1

Knaģis
Knaģis

Reputation: 21495

Use Console.WriteLine(ex.GetType().FullName) (or put a breakpoint and run under a debugger) to see the actual exception type being thrown. Then visit MSDN to see its description and base classes. You need to decide which of the base classes provides you with the information needed by exposing such properties. Then use that class in your catch() expression.

For Entity Framework, you might end up with using EntityException and then checking the InnerException property for the SQL exception object that it wraps.

try
{
    dbconn.table.AddObject(newRow);
    dbconn.SaveChanges();
}
catch (EntityException ex)
{
    Console.WriteLine("DB fail ID:" + Row.id + "; Error: " + ex.Message);
    var sqlExc = ex.InnerException as SqlException;
    if (sqlExc != null)
        Console.WriteLine("SQL error code: " + sqlExc.Number);
}

Upvotes: 1

Thomas Weller
Thomas Weller

Reputation: 11717

You should also output the exception. Most of the time, it holds useful and detailed information (e.g. names of violated constraints). Try this:

try
{
    dbconn.table.AddObject(newRow);
    dbconn.SaveChanges();
}
catch (Exception ex)
{
    Console.WriteLine("DB fail ID:" + Row.id);
    Console.WriteLine(ex.ToString());
}

For full details, use the ToString() method, it will give you the stack trace as well, not only the error message.

Upvotes: 1

Related Questions