mechanicum
mechanicum

Reputation: 709

standard sql error message to console too vague

Is there a way to make console display of exception messages a little bit more precise in a c# winforms app. As in, instead of just;

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Perhaps more like java's,

java.sql.SQLException: Closed Connection at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at
...

At least so I have an idea about what may be wrong. I'm sure there is something for this situation but got no idea:)

Upvotes: 1

Views: 267

Answers (3)

Remus Rusanu
Remus Rusanu

Reputation: 294287

Yes, sure it is:

try
{
  /// your code goes here ...
}
catch(SqlException sex)
{
 foreach(SqlError error in sex)
 {
   Console.WriteLine("{0}:{1} Error {2} State {3}: \"{4}\" at {5}@{6}", 
     error.Server, error.Source,
     error.Number, error.State,
     error.Message,
     error.Procedure, error.LineNumber);
 }
}

You got it? try ... catch !

For the (few) cases where is valid to not have a try/catch block you can resort to the global handlers: Application.UnhandledException for UI message pump threads and AppDomain.UnhandledException for non-UI threads.

Upvotes: 0

sim1
sim1

Reputation: 720

SQLException already contains enough information

Please have a look at the MSDN page (http://msdn.microsoft.com/it-it/library/system.data.sqlclient.sqlexception.aspx)

 try
        {
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            for (int i = 0; i < ex.Errors.Count; i++)
            {
                errorMessages.Append("Index #" + i + "\n" +
                    "Message: " + ex.Errors[i].Message + "\n" +
                    "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                    "Source: " + ex.Errors[i].Source + "\n" +
                    "Procedure: " + ex.Errors[i].Procedure + "\n");
            }
            Console.WriteLine(errorMessages.ToString());
        }

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062865

That extra information is basically .StackTrace. How precise it can be depends on how it was built, whether the pdb is available, etc.

If you just do something like:

catch(Exception ex) {
    Console.Error.WriteLine(ex);
}

you should see a similar level of information by default. Basically, the ToString() shows more than just the .Message. For example:

static void Main()
{
    try {Throw();}
    catch(Exception ex) {
        Console.Error.WriteLine(ex);
    }
}
public static void Throw()
{
    throw new InvalidOperationException("oops");
}

displays:

System.InvalidOperationException: oops
   at X.Throw() in c:\Users\Marc\Documents\Visual Studio 2012\Projects\ConsoleAp
plication1\ConsoleApplication1\Program.cs:line 30
   at X.Main() in c:\Users\Marc\Documents\Visual Studio 2012\Projects\ConsoleApp
lication1\ConsoleApplication1\Program.cs:line 20

Upvotes: 2

Related Questions