未捕获MySQL异常(C#)

未捕获MySQL异常(C#),c#,mysql,connection-string,C#,Mysql,Connection String,我的C#程序使用MySQL数据库 由于某些原因,程序无法捕获MySQL连接引起的异常 例如: 如果我使连接字符串中的凭据无效,程序会像这样崩溃(即使在调试器中运行): 连接代码如下所示: using MySQLDriverCS; namespace XXX { public class Data { private static MySQLConnection con; static Data() { s

我的C#程序使用MySQL数据库

由于某些原因,程序无法捕获MySQL连接引起的异常

例如:

如果我使连接字符串中的凭据无效,程序会像这样崩溃(即使在调试器中运行):

连接代码如下所示:

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();
        }
...
关于如何改进事情并开始捕捉MySQL异常,有什么想法吗


我已经尝试过用try-catch将代码包装在静态构造函数中。那没用。程序仍然以同样的方式崩溃

谢谢


与try-catch包装相同的代码。它仍然会失败,并出现相同的错误:


在C#中捕获或处理异常通常需要
try-catch
语句

语法基本上如下所示:

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

Console.Read();
因此,将您的
con.Open()
逻辑包装在
try catch
语句中,如下所示:

try
{
    Con.Open();
} 
catch (Exception e){
    // handle error
    Console.WriteLine("Possible MySQL Exception: " + e);
}
此外,您可以将
finally
块添加到
try catch
语句的末尾,该语句执行其代码,而不管是否处理了异常:

    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();

在catch块中使用适当的异常类型

使用适当的MySQL类

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();
        }
    }
}
发现错误没有问题:

添加引用屏幕截图:


尝试将连接字符串传递到mysqlconnection对象,而不是使用mysqlconnectionstring对象“exception not catch”,因为没有Try catch块。尝试添加Try-catch块,您可以看到错误。我已经尝试将代码包装在Try-catch中的静态构造函数中。那没用。程序仍以相同的方式崩溃。您会收到此错误,因为它正在运行托管,并且未为非托管代码配置调试器。转到项目的属性并检查“调试”和“生成”选项卡。您应该能够在那里启用非托管代码调试。那没用。程序仍然以同样的方式崩溃。我将尝试破坏我的连接。没有问题捕获我的错误。上面以白色显示的错误陷阱屏幕有时人们忘记了他们是为了调试而编译的,而是运行旧的版本代码。idk你正在做什么,或者你的参考资料或课程。祝你好运:用pUsing MySql.Data.MySqlClient代替MySQLDriverCS帮了大忙。现在我有一个例外。非常感谢我想现在是时候进行一些代码重构了。我已经尝试过用try-catch将代码包装在静态构造函数中。那没用。程序仍然以同样的方式崩溃。
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();
        }
    }
}