Try catch не перехватывает SQLiteException - C#

Узнай цену своей работы

Формулировка задачи:

Имеется такой метод:
Листинг программы
  1. bool UpdateData(SQLiteDataAdapter DA, DataTable dt)
  2. {
  3. try
  4. {
  5. DA.Update(dt);
  6. return true;
  7. }
  8. catch (SQLiteException ex)
  9. {
  10. MessageBox.Show(ex.Message, "Ошибка при сохранении данных.", MessageBoxButtons.OK, MessageBoxIcon.Error);
  11. return false;
  12. }
  13. }
При его выполнении иногда вылетает исключение "SQLiteException не обработано пользовательским кодом. constraint failed FOREIGN KEY constraint failed". Почему это исключение не перехватывает catch?

Решение задачи: «Try catch не перехватывает SQLiteException»

textual
Листинг программы
  1. [ToolboxItem("SQLite.Designer.SQLiteDataAdapterToolboxItem, SQLite.Designer, Version=1.0.97.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"), DefaultEvent("RowUpdated"), Designer("Microsoft.VSDesigner.Data.VS.SqlDataAdapterDesigner, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  2. public sealed class SQLiteDataAdapter : DbDataAdapter
  3. {
  4.     // Fields
  5.     private static object _updatedEventPH = new object();
  6.     private static object _updatingEventPH = new object();
  7.     private bool disposed;
  8.     private bool disposeSelect;
  9.  
  10.     // Events
  11.     public event EventHandler<RowUpdatedEventArgs> RowUpdated
  12.     {
  13.         add
  14.         {
  15.             this.CheckDisposed();
  16.             base.Events.AddHandler(_updatedEventPH, value);
  17.         }
  18.         remove
  19.         {
  20.             this.CheckDisposed();
  21.             base.Events.RemoveHandler(_updatedEventPH, value);
  22.         }
  23.     }
  24.  
  25.     public event EventHandler<RowUpdatingEventArgs> RowUpdating
  26.     {
  27.         add
  28.         {
  29.             this.CheckDisposed();
  30.             EventHandler<RowUpdatingEventArgs> mcd = (EventHandler<RowUpdatingEventArgs>) base.Events[_updatingEventPH];
  31.             if ((mcd != null) && (value.Target is DbCommandBuilder))
  32.             {
  33.                 EventHandler<RowUpdatingEventArgs> handler = (EventHandler<RowUpdatingEventArgs>) FindBuilder(mcd);
  34.                 if (handler != null)
  35.                 {
  36.                     base.Events.RemoveHandler(_updatingEventPH, handler);
  37.                 }
  38.             }
  39.             base.Events.AddHandler(_updatingEventPH, value);
  40.         }
  41.         remove
  42.         {
  43.             this.CheckDisposed();
  44.             base.Events.RemoveHandler(_updatingEventPH, value);
  45.         }
  46.     }
  47.  
  48.     // Methods
  49.     public SQLiteDataAdapter()
  50.     {
  51.         this.disposeSelect = true;
  52.     }
  53.  
  54.     public SQLiteDataAdapter(SQLiteCommand cmd)
  55.     {
  56.         this.disposeSelect = true;
  57.         this.SelectCommand = cmd;
  58.         this.disposeSelect = false;
  59.     }
  60.  
  61.     public SQLiteDataAdapter(string commandText, SQLiteConnection connection)
  62.     {
  63.         this.disposeSelect = true;
  64.         this.SelectCommand = new SQLiteCommand(commandText, connection);
  65.     }
  66.  
  67.     public SQLiteDataAdapter(string commandText, string connectionString) : this(commandText, connectionString, false)
  68.     {
  69.     }
  70.  
  71.     public SQLiteDataAdapter(string commandText, string connectionString, bool parseViaFramework)
  72.     {
  73.         this.disposeSelect = true;
  74.         SQLiteConnection connection = new SQLiteConnection(connectionString, parseViaFramework);
  75.         this.SelectCommand = new SQLiteCommand(commandText, connection);
  76.     }
  77.  
  78.     private void CheckDisposed()
  79.     {
  80.         if (this.disposed)
  81.         {
  82.             throw new ObjectDisposedException(typeof(SQLiteDataAdapter).Name);
  83.         }
  84.     }
  85.  
  86.     protected override void Dispose(bool disposing)
  87.     {
  88.         try
  89.         {
  90.             if (!this.disposed && disposing)
  91.             {
  92.                 if (this.disposeSelect && (this.SelectCommand != null))
  93.                 {
  94.                     this.SelectCommand.Dispose();
  95.                     this.SelectCommand = null;
  96.                 }
  97.                 if (this.InsertCommand != null)
  98.                 {
  99.                     this.InsertCommand.Dispose();
  100.                     this.InsertCommand = null;
  101.                 }
  102.                 if (this.UpdateCommand != null)
  103.                 {
  104.                     this.UpdateCommand.Dispose();
  105.                     this.UpdateCommand = null;
  106.                 }
  107.                 if (this.DeleteCommand != null)
  108.                 {
  109.                     this.DeleteCommand.Dispose();
  110.                     this.DeleteCommand = null;
  111.                 }
  112.             }
  113.         }
  114.         finally
  115.         {
  116.             base.Dispose(disposing);
  117.             this.disposed = true;
  118.         }
  119.     }
  120.  
  121.     internal static Delegate FindBuilder(MulticastDelegate mcd)
  122.     {
  123.         if (mcd != null)
  124.         {
  125.             Delegate[] invocationList = mcd.GetInvocationList();
  126.             for (int i = 0; i < invocationList.Length; i++)
  127.             {
  128.                 if (invocationList[i].Target is DbCommandBuilder)
  129.                 {
  130.                     return invocationList[i];
  131.                 }
  132.             }
  133.         }
  134.         return null;
  135.     }
  136.  
  137.     protected override void OnRowUpdated(RowUpdatedEventArgs value)
  138.     {
  139.         EventHandler<RowUpdatedEventArgs> handler = base.Events[_updatedEventPH] as EventHandler<RowUpdatedEventArgs>;
  140.         if (handler != null)
  141.         {
  142.             handler(this, value);
  143.         }
  144.     }
  145.  
  146.     protected override void OnRowUpdating(RowUpdatingEventArgs value)
  147.     {
  148.         EventHandler<RowUpdatingEventArgs> handler = base.Events[_updatingEventPH] as EventHandler<RowUpdatingEventArgs>;
  149.         if (handler != null)
  150.         {
  151.             handler(this, value);
  152.         }
  153.     }
  154.  
  155.     // Properties
  156.     [Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultValue((string) null)]
  157.     public SQLiteCommand DeleteCommand
  158.     {
  159.         get
  160.         {
  161.             this.CheckDisposed();
  162.             return (SQLiteCommand) base.DeleteCommand;
  163.         }
  164.         set
  165.         {
  166.             this.CheckDisposed();
  167.             base.DeleteCommand = value;
  168.         }
  169.     }
  170.  
  171.     [Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultValue((string) null)]
  172.     public SQLiteCommand InsertCommand
  173.     {
  174.         get
  175.         {
  176.             this.CheckDisposed();
  177.             return (SQLiteCommand) base.InsertCommand;
  178.         }
  179.         set
  180.         {
  181.             this.CheckDisposed();
  182.             base.InsertCommand = value;
  183.         }
  184.     }
  185.  
  186.     [Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultValue((string) null)]
  187.     public SQLiteCommand SelectCommand
  188.     {
  189.         get
  190.         {
  191.             this.CheckDisposed();
  192.             return (SQLiteCommand) base.SelectCommand;
  193.         }
  194.         set
  195.         {
  196.             this.CheckDisposed();
  197.             base.SelectCommand = value;
  198.         }
  199.     }
  200.  
  201.     [DefaultValue((string) null), Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  202.     public SQLiteCommand UpdateCommand
  203.     {
  204.         get
  205.         {
  206.             this.CheckDisposed();
  207.             return (SQLiteCommand) base.UpdateCommand;
  208.         }
  209.         set
  210.         {
  211.             this.CheckDisposed();
  212.             base.UpdateCommand = value;
  213.         }
  214.     }
  215. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 4.154 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы