Try catch не перехватывает SQLiteException - C#
Формулировка задачи:
Имеется такой метод:
При его выполнении иногда вылетает исключение "SQLiteException не обработано пользовательским кодом. constraint failed FOREIGN KEY constraint failed".
Почему это исключение не перехватывает catch?
Листинг программы
- bool UpdateData(SQLiteDataAdapter DA, DataTable dt)
- {
- try
- {
- DA.Update(dt);
- return true;
- }
- catch (SQLiteException ex)
- {
- MessageBox.Show(ex.Message, "Ошибка при сохранении данных.", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return false;
- }
- }
Решение задачи: «Try catch не перехватывает SQLiteException»
textual
Листинг программы
- [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")]
- public sealed class SQLiteDataAdapter : DbDataAdapter
- {
- // Fields
- private static object _updatedEventPH = new object();
- private static object _updatingEventPH = new object();
- private bool disposed;
- private bool disposeSelect;
- // Events
- public event EventHandler<RowUpdatedEventArgs> RowUpdated
- {
- add
- {
- this.CheckDisposed();
- base.Events.AddHandler(_updatedEventPH, value);
- }
- remove
- {
- this.CheckDisposed();
- base.Events.RemoveHandler(_updatedEventPH, value);
- }
- }
- public event EventHandler<RowUpdatingEventArgs> RowUpdating
- {
- add
- {
- this.CheckDisposed();
- EventHandler<RowUpdatingEventArgs> mcd = (EventHandler<RowUpdatingEventArgs>) base.Events[_updatingEventPH];
- if ((mcd != null) && (value.Target is DbCommandBuilder))
- {
- EventHandler<RowUpdatingEventArgs> handler = (EventHandler<RowUpdatingEventArgs>) FindBuilder(mcd);
- if (handler != null)
- {
- base.Events.RemoveHandler(_updatingEventPH, handler);
- }
- }
- base.Events.AddHandler(_updatingEventPH, value);
- }
- remove
- {
- this.CheckDisposed();
- base.Events.RemoveHandler(_updatingEventPH, value);
- }
- }
- // Methods
- public SQLiteDataAdapter()
- {
- this.disposeSelect = true;
- }
- public SQLiteDataAdapter(SQLiteCommand cmd)
- {
- this.disposeSelect = true;
- this.SelectCommand = cmd;
- this.disposeSelect = false;
- }
- public SQLiteDataAdapter(string commandText, SQLiteConnection connection)
- {
- this.disposeSelect = true;
- this.SelectCommand = new SQLiteCommand(commandText, connection);
- }
- public SQLiteDataAdapter(string commandText, string connectionString) : this(commandText, connectionString, false)
- {
- }
- public SQLiteDataAdapter(string commandText, string connectionString, bool parseViaFramework)
- {
- this.disposeSelect = true;
- SQLiteConnection connection = new SQLiteConnection(connectionString, parseViaFramework);
- this.SelectCommand = new SQLiteCommand(commandText, connection);
- }
- private void CheckDisposed()
- {
- if (this.disposed)
- {
- throw new ObjectDisposedException(typeof(SQLiteDataAdapter).Name);
- }
- }
- protected override void Dispose(bool disposing)
- {
- try
- {
- if (!this.disposed && disposing)
- {
- if (this.disposeSelect && (this.SelectCommand != null))
- {
- this.SelectCommand.Dispose();
- this.SelectCommand = null;
- }
- if (this.InsertCommand != null)
- {
- this.InsertCommand.Dispose();
- this.InsertCommand = null;
- }
- if (this.UpdateCommand != null)
- {
- this.UpdateCommand.Dispose();
- this.UpdateCommand = null;
- }
- if (this.DeleteCommand != null)
- {
- this.DeleteCommand.Dispose();
- this.DeleteCommand = null;
- }
- }
- }
- finally
- {
- base.Dispose(disposing);
- this.disposed = true;
- }
- }
- internal static Delegate FindBuilder(MulticastDelegate mcd)
- {
- if (mcd != null)
- {
- Delegate[] invocationList = mcd.GetInvocationList();
- for (int i = 0; i < invocationList.Length; i++)
- {
- if (invocationList[i].Target is DbCommandBuilder)
- {
- return invocationList[i];
- }
- }
- }
- return null;
- }
- protected override void OnRowUpdated(RowUpdatedEventArgs value)
- {
- EventHandler<RowUpdatedEventArgs> handler = base.Events[_updatedEventPH] as EventHandler<RowUpdatedEventArgs>;
- if (handler != null)
- {
- handler(this, value);
- }
- }
- protected override void OnRowUpdating(RowUpdatingEventArgs value)
- {
- EventHandler<RowUpdatingEventArgs> handler = base.Events[_updatingEventPH] as EventHandler<RowUpdatingEventArgs>;
- if (handler != null)
- {
- handler(this, value);
- }
- }
- // Properties
- [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)]
- public SQLiteCommand DeleteCommand
- {
- get
- {
- this.CheckDisposed();
- return (SQLiteCommand) base.DeleteCommand;
- }
- set
- {
- this.CheckDisposed();
- base.DeleteCommand = value;
- }
- }
- [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)]
- public SQLiteCommand InsertCommand
- {
- get
- {
- this.CheckDisposed();
- return (SQLiteCommand) base.InsertCommand;
- }
- set
- {
- this.CheckDisposed();
- base.InsertCommand = value;
- }
- }
- [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)]
- public SQLiteCommand SelectCommand
- {
- get
- {
- this.CheckDisposed();
- return (SQLiteCommand) base.SelectCommand;
- }
- set
- {
- this.CheckDisposed();
- base.SelectCommand = value;
- }
- }
- [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")]
- public SQLiteCommand UpdateCommand
- {
- get
- {
- this.CheckDisposed();
- return (SQLiteCommand) base.UpdateCommand;
- }
- set
- {
- this.CheckDisposed();
- base.UpdateCommand = value;
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д