{"id":1832,"date":"2022-08-30T15:19:49","date_gmt":"2022-08-30T15:19:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/c-winforms-unable-to-catch-sqliteexception-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:49","modified_gmt":"2022-08-30T15:19:49","slug":"c-winforms-unable-to-catch-sqliteexception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/c-winforms-unable-to-catch-sqliteexception-collection-of-common-programming-errors\/","title":{"rendered":"C# WinForms Unable to catch SQLiteException-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve been trying to add exception handling to my test app, but with no success. My app uses SQLite as database provider.<\/p>\n<p>I&#8217;m tring to catch SQLiteException:<\/p>\n<pre><code>    static void Main()\n    {\n        Application.EnableVisualStyles();\n        Application.SetCompatibleTextRenderingDefault(false);\n\n        try\n        {\n            Application.Run(new Main());\n        }\n        catch (System.Data.SQLite.SQLiteException)\n        {\n            MessageBox.Show(\"Unable to perform database operation. Database file is missing or corrupt\", \"Database error\", MessageBoxButtons.OK, MessageBoxIcon.Error);\n        }\n    }\n<\/code><\/pre>\n<p>In my Main form I&#8217;ve got such lines:<\/p>\n<pre><code>        using (DbWrapper db = DbFactory.GetConnection(\"SQLite\").WrapIn())\n        {\n            db.Open();\n\n            count = db.ExecuteScalar(\"SELECT COUNT(*) FROM subject WHERE schedule_id = \" + ScheduleList.SelectedValue);\n        }\n<\/code><\/pre>\n<p>However, if I delete my database file from working directory I&#8217;m still getting unhandled exceptions instead of MessageBox window.<\/p>\n<blockquote>\n<p>System.Data.SQLite.SQLiteException (0x80004005): unable to open database file<\/p>\n<\/blockquote>\n<p>I&#8217;ve also tried catching Exception type and AppDomain.CurrentDomain.UnhandledException event, but still got the same behaviour.<\/p>\n<p>I&#8217;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?<\/p>\n<p><strong>UPDATE:<\/strong> Exception handling works perfectly if I wrap my sql operation code in try-catch block right inside my form method.<\/p>\n<p><strong>UPDATE: Problem was solved by adding ThreadExceptionEventHandler. Any ideas how to catch SQLiteException directly?<\/strong><\/p>\n<p>Best Regards<\/p>\n<ol>\n<li>\n<p>Sqlite .NET libraries are ultimately managed wrappers around unmanaged c++ Sqlite assemblies. Although it would be informative to get the actual driver you&#8217;re using, I am assuming that when you added Sqlite to your project (nuget?) it also added an interop assembly.<\/p>\n<p>When you&#8217;re dealing with managed wrappers around unmanaged libraries you can sometimes run into cases where you can&#8217;t catch the exceptions being thrown in the unmanaged code. Sometimes adding the legacy unhandled exception policy in your app.config works but it didn&#8217;t for me.<\/p>\n<p>http:\/\/msdn.microsoft.com\/en-us\/library\/ms228965(v=vs.100).aspx<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 20:56:42. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been trying to add exception handling to my test app, but with no success. My app uses SQLite as database provider. I&#8217;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(&#8220;Unable to perform database operation. Database file is missing or corrupt&#8221;, &#8220;Database error&#8221;, MessageBoxButtons.OK, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1832","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1832"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1832\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}