I've been trying to add exception handling to my test app, but with no success. My app uses SQLite as database provider.
I'm tring to catch SQLiteException:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
Application.Run(new Main());
}
catch (System.Data.SQLite.SQLiteException)
{
MessageBox.Show("Unable to perform database operation. Database file is missing or corrupt", "Database error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
In my Main form I've got such lines:
using (DbWrapper db = DbFactory.GetConnection("SQLite").WrapIn<MyDbWrapper>())
{
db.Open();
count = db.ExecuteScalar<int>("SELECT COUNT(*) FROM subject WHERE schedule_id = " + ScheduleList.SelectedValue);
}
However, if I delete my database file from working directory I'm still getting unhandled exceptions instead of MessageBox window.
System.Data.SQLite.SQLiteException (0x80004005): unable to open database file
I've also tried catching Exception type and AppDomain.CurrentDomain.UnhandledException event, but still got the same behaviour.
I'm not asking for solution, just wondering why is it acting like this? Why am I unable to catch the exception? May it be caused by GC corruption while encountering an exception in using block?
UPDATE: Exception handling works perfectly if I wrap my sql operation code in try-catch block right inside my form method.
UPDATE: Problem was solved by adding ThreadExceptionEventHandler. Any ideas how to catch SQLiteException directly?
Best Regards