1

I am try do some error handling when it comes to connecting to a MySQL database. I want to separate out connection errors and SQL errors. Issue being is the error that is being returned doesn't seem to be a MySQL error but in fact a MS error. This is what I am doing:

using (MySqlConnection con = new MySqlConnection(ConString))
{
    MySqlDataAdapter da = new MySqlDataAdapter();
    MySqlCommand cmd = new MySqlCommand(sqlCMD, con);
    try
    {
        System.Security.Cryptography.RSACryptoServiceProvider.UseMachineKeyStore = true;
        var provider = new System.Security.Cryptography.RSACryptoServiceProvider();
        con.Open();
        da.SelectCommand = cmd;
        da.Fill(dt);
        con.Close();
    }
    catch (Exception x)
    {
        //Amazing error catching code
    }
}

Now lets say I try to connect to a machine that does not have MySQL installed, this is the exception that being returned:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. at MySql.Data.MySqlClient.NativeDriver.Open() at MySql.Data.MySqlClient.Driver.Open() at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() at MySql.Data.MySqlClient.MySqlPool.GetConnection() at MySql.Data.MySqlClient.MySqlConnection.Open()

Now googling "0x80004005 MySQL" returns multiple threads on Stack Overflow about C# connecting to MySQL. Just doing a search for 0x80004005 returns a Windows XP error message page.

So that leaves me with a question what is the best way to determine a connection error vs a SQL error message?

Xaphann
  • 3,195
  • 10
  • 42
  • 70
  • 1
    Why not perform catches on the different Exception Types that MySQL connector can thrown. (Not exact Exception types) like try{}catch(MySqlConnectionException){}catch(MySqlQueryException ). You can catch and handle more than 1 type of exception, especially a general exception. Other than this you would need to drill into the General Exception and determine the underlying exception type. Take a look at this example as well https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-errors.html – Bearcat9425 Jun 10 '16 at 21:07
  • @Bearcat9425 too bad you answered this as comment and not an actual answer. I would have given you the credit. As it is all I can do is up vote the comment. If you do answer next time I check will give you the answer. – Xaphann Jun 13 '16 at 15:45
  • 1
    Posted my comment as an answer for reference to anyone else who comes upon this question. I know you have marked already an answer but figured I would put my version of the answer in. – Bearcat9425 Jun 13 '16 at 17:51
  • 1
    I upvoted your question. Hope your excellent codes ! – Kay Lee Jun 14 '16 at 06:27

3 Answers3

3

Why not perform catches on the different Exception Types that MySQL connector can thrown. (Not exact Exception types) like

try
{
}catch(MySqlConnectionException)
{
}catch(MySqlQueryException )
{
}

You can catch and handle more than 1 type of exception, especially a general exception. Other than this you would need to drill into the General Exception and determine the underlying exception type. Take a look at this example as well

https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-errors.html

Bearcat9425
  • 1,580
  • 1
  • 11
  • 12
1

I am not aware of MySQL Exceptions, but you need to use more catch blocks.

just go to definition of each method you used and there you can see what exceptions could be thrown by each method. then instead of using

catch (Exception x)
    {
        //Amazing error catching code
    }

use multiple catch blocks, one after another. see this post for how to group exceptions if there are two many.

Catch multiple exceptions at once?

Community
  • 1
  • 1
eulerleibniz
  • 271
  • 3
  • 11
1

Just consider this as simple idea and if not needed, just ignore..There might be official way unlike this..

catch (MySqlException ex)
{
  if (ex.Message.Contains("connection"))
     {
       // do stuff when connection trouble
     }
  else
    {
       // do stuff when MySql trouble
    }
}

Hope this helps..

Kay Lee
  • 922
  • 1
  • 12
  • 40