@@ -3069,25 +3069,29 @@ public int ExecuteNonQuery ()
30693069 _conn . Tracer ? . Invoke ( "Executing: " + this ) ;
30703070 }
30713071
3072- var r = SQLite3 . Result . OK ;
30733072 var stmt = Prepare ( ) ;
3074- r = SQLite3 . Step ( stmt ) ;
3075- Finalize ( stmt ) ;
3076- if ( r == SQLite3 . Result . Done ) {
3077- int rowsAffected = SQLite3 . Changes ( _conn . Handle ) ;
3078- return rowsAffected ;
3079- }
3080- else if ( r == SQLite3 . Result . Error ) {
3081- string msg = SQLite3 . GetErrmsg ( _conn . Handle ) ;
3082- throw SQLiteException . New ( r , msg ) ;
3083- }
3084- else if ( r == SQLite3 . Result . Constraint ) {
3085- if ( SQLite3 . ExtendedErrCode ( _conn . Handle ) == SQLite3 . ExtendedResult . ConstraintNotNull ) {
3086- throw NotNullConstraintViolationException . New ( r , SQLite3 . GetErrmsg ( _conn . Handle ) ) ;
3073+ try {
3074+ // FIX: Use try/finally to ensure Finalize runs, and throw exceptions
3075+ // BEFORE finally block so GetErrmsg is called while error state is valid.
3076+ // See: https://github.com/praeclarum/sqlite-net/issues/1041
3077+ var r = SQLite3 . Step ( stmt ) ;
3078+ if ( r == SQLite3 . Result . Done || r == SQLite3 . Result . Row ) {
3079+ int rowsAffected = SQLite3 . Changes ( _conn . Handle ) ;
3080+ return rowsAffected ;
3081+ }
3082+ else if ( r == SQLite3 . Result . Error ) {
3083+ throw SQLiteException . New ( r , SQLite3 . GetErrmsg ( _conn . Handle ) ) ;
3084+ }
3085+ else if ( r == SQLite3 . Result . Constraint ) {
3086+ if ( SQLite3 . ExtendedErrCode ( _conn . Handle ) == SQLite3 . ExtendedResult . ConstraintNotNull ) {
3087+ throw NotNullConstraintViolationException . New ( r , SQLite3 . GetErrmsg ( _conn . Handle ) ) ;
3088+ }
30873089 }
3090+ throw SQLiteException . New ( r , SQLite3 . GetErrmsg ( _conn . Handle ) ) ;
3091+ }
3092+ finally {
3093+ Finalize ( stmt ) ;
30883094 }
3089-
3090- throw SQLiteException . New ( r , SQLite3 . GetErrmsg ( _conn . Handle ) ) ;
30913095 }
30923096
30933097 public IEnumerable < T > ExecuteDeferredQuery < T > ( )
@@ -3748,8 +3752,6 @@ public int ExecuteNonQuery (object[] source)
37483752 Connection . Tracer ? . Invoke ( "Executing: " + CommandText ) ;
37493753 }
37503754
3751- var r = SQLite3 . Result . OK ;
3752-
37533755 if ( ! Initialized ) {
37543756 Statement = SQLite3 . Prepare2 ( Connection . Handle , CommandText ) ;
37553757 Initialized = true ;
@@ -3761,25 +3763,28 @@ public int ExecuteNonQuery (object[] source)
37613763 SQLiteCommand . BindParameter ( Statement , i + 1 , source [ i ] , Connection . StoreDateTimeAsTicks , Connection . DateTimeStringFormat , Connection . StoreTimeSpanAsTicks ) ;
37623764 }
37633765 }
3764- r = SQLite3 . Step ( Statement ) ;
37653766
3766- if ( r == SQLite3 . Result . Done ) {
3767- int rowsAffected = SQLite3 . Changes ( Connection . Handle ) ;
3768- SQLite3 . Reset ( Statement ) ;
3769- return rowsAffected ;
3770- }
3771- else if ( r == SQLite3 . Result . Error ) {
3772- string msg = SQLite3 . GetErrmsg ( Connection . Handle ) ;
3773- SQLite3 . Reset ( Statement ) ;
3774- throw SQLiteException . New ( r , msg ) ;
3775- }
3776- else if ( r == SQLite3 . Result . Constraint && SQLite3 . ExtendedErrCode ( Connection . Handle ) == SQLite3 . ExtendedResult . ConstraintNotNull ) {
3777- SQLite3 . Reset ( Statement ) ;
3778- throw NotNullConstraintViolationException . New ( r , SQLite3 . GetErrmsg ( Connection . Handle ) ) ;
3767+ try {
3768+ // FIX: Use try/finally to ensure Reset runs, and throw exceptions
3769+ // BEFORE finally block so GetErrmsg is called while error state is valid.
3770+ // See: https://github.com/praeclarum/sqlite-net/issues/1041
3771+ var r = SQLite3 . Step ( Statement ) ;
3772+ if ( r == SQLite3 . Result . Done || r == SQLite3 . Result . Row ) {
3773+ int rowsAffected = SQLite3 . Changes ( Connection . Handle ) ;
3774+ return rowsAffected ;
3775+ }
3776+ else if ( r == SQLite3 . Result . Error ) {
3777+ throw SQLiteException . New ( r , SQLite3 . GetErrmsg ( Connection . Handle ) ) ;
3778+ }
3779+ else if ( r == SQLite3 . Result . Constraint ) {
3780+ if ( SQLite3 . ExtendedErrCode ( Connection . Handle ) == SQLite3 . ExtendedResult . ConstraintNotNull ) {
3781+ throw NotNullConstraintViolationException . New ( r , SQLite3 . GetErrmsg ( Connection . Handle ) ) ;
3782+ }
3783+ }
3784+ throw SQLiteException . New ( r , SQLite3 . GetErrmsg ( Connection . Handle ) ) ;
37793785 }
3780- else {
3786+ finally {
37813787 SQLite3 . Reset ( Statement ) ;
3782- throw SQLiteException . New ( r , SQLite3 . GetErrmsg ( Connection . Handle ) ) ;
37833788 }
37843789 }
37853790
0 commit comments