MySQL exceptions not caught (C#)

二次信任 提交于 2019-12-17 20:53:30

问题


My C# program works with a MySQL database.

For some reason the program cannot catch exceptions caused my the MySQL connection.

Example:

If I make the credentials in the connection string invalid, the program crashes like this (even when running in the debugger): http://imgur.com/SfzkVdW

The connection code is like this:

using MySQLDriverCS;

namespace XXX
{
    public class Data
    {
        private static MySQLConnection con;

        static Data()
        {
            string connectionString = new MySQLConnectionString("XXX",
                "XXX",
                "XXX",
                "XXX").AsString;

            con = new MySQLConnection(connectionString + ";CharSet=utf8");
            con.Open(); // For testing the connection
            con.Close();
        }
...

Any ideas for how I can improve things and start catching MySQL exceptions?

I have tried wrapping the code in the static constructor in a try-catch. That didn't help. The program still crashed in the same way.

Thanks.


Same code with the try-catch wrapper. It still fails with the same error: http://imgur.com/SfzkVdW

    static Data()
    {
        try
        {
            string connectionString = new MySQLConnectionString("XXX",
                "XXX",
                "XXX",
                "XXX").AsString;

            con = new MySQLConnection(connectionString + ";CharSet=utf8");
            con.Open(); // For testing the connection
            con.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

回答1:


Use the appropriate exception type in the catch block.

Use the appropriate MySQL classes.

using MySql.Data.MySqlClient;

// class level var or whatnot:
string connString = @"server=theHostName;userid=dbuser123;password=OpenSesame7;database=my_db_name";


public void connect()
{
    try
    {
        conn = new MySqlConnection(connString); // read above comments for (conn)
        conn.Open();
    }
    catch (MySqlException ex)
    {
        MessageBoxButtons buttons = MessageBoxButtons.OK;
        string s="MySqlException: "+ex.ToString();
        MessageBox.Show(s,"Error",buttons);
    }
    finally
    {
        if (conn != null)
        {
            //conn.Close();
        }
    }
}

Error Caught No Problem:

Add References screenshot:




回答2:


Catching or handling exceptions in C# generally requires a try-catch statement.

The syntax is essentially as follows:

try
{
    // operation likely to cause error
} 
catch (Exception e){
    // handle error
    Console.WriteLine("Exception: " + e);
}

Console.Read();

So wrap your con.Open() logic in a try-catch statement like so:

try
{
    Con.Open();
} 
catch (Exception e){
    // handle error
    Console.WriteLine("Possible MySQL Exception: " + e);
}

Additionally, you can add a finally block to the end of a try-catch statement, which executes its code regardless of whether an exception was handled or not:

    try
{
    // attempt to do something here
    con.Open();
} 
catch (Exception e){
    // handle error
    Console.Writeline("Exception: " + e);
}
finally 
{
    Console.Writeline("This runs no matter if an exception is thrown or not!");
}

Console.Read();


来源:https://stackoverflow.com/questions/38277564/mysql-exceptions-not-caught-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!