1
我正在连接到数据库,并且在使用无效凭据对其进行测试时出现异常。 连接的代码是在一个单独的类,我已经在那里捕捉错误。如何处理此异常
但它似乎经历了这一点,并有另一个异常指向我在哪里调用我的mainWindow.xaml.cs构造函数。我如何处理第二个异常。我在我的MainWindow页面做了另一个try/catch,异常仍然存在。请指教。
//Code at class SimpleDataSource which does the connection:
public void Connect(string server, string database, int port, string user, string password)
{
// TODO: Initialise MySqlConnection object with parameters,
// open connection with suitable exception handling.
string connStr = "server=" + server + ";database=" + database + ";port=" + port + ";user=" + user + ";password=" + password + ";";
try
{
conn = new MySqlConnection(connStr);
conn.Open();
}
catch (MySqlException e)
{
MessageBox.Show(@"Unable to connect to database.
Please check the following and try again.
1. Ensure you have an internet connection.
2. Ensure your credentials are entered correctly");
Console.WriteLine("The error is " + e);
}
}
//Calling the constructor for that class on my MainWindow which is causing the exception:
SimpleDataSource dataSource;
try { dataSource = new SimpleDataSource("111.111.11.11", "eeeee", 3306, "eeeee", "eeee"); }
catch (MySqlException e) { e.StackTrace.ToString();}
错误消息: 我试图交换为XamlParseException代替个MySqlException和相同的结果。
你的构造函数中的东西抛出一个异常。如果您认为这是上述方法,请在第一行放置一个断点并逐步完成代码。或者在catch(MySqlException e)之后,为其他类型的异常添加另一个catch,并将其输出到控制台,以便查看它是什么。 –
在您的例外对话框中,点击'Viewdetail'查看错误原因。当某些xaml文件使用不正确的xaml标记或属性值时,此异常(XamlParseException)会在应用程序加载时出现。您需要确保您的xaml文件具有正确的语法。 – VS1
@ VS1来仔细检查,你的意思是我的错误是在我的MainWindow.xaml页面本身而不是我的类文件? –