How to catch exception in sqlite transaction - java android

SO I am writing a database manager for android, and in this class there will be a method for writing to the table of this database.

SQLiteDatabase db = this.getReadableDatabase(); db.beginTransaction(); try { //DO some db writing db.setTransactionSuccessful(); } finally { db.endTransaction(); } 

I understand that this will automatically roll back the data if the transaction is not successful, as it is in an attempt, but how will I catch if it was not successful, so I can notify the user of the successful completion or rollback

+5
source share
2 answers

just put the catch block before the finally block

 SQLiteDatabase db = this.getReadableDatabase(); db.beginTransaction(); try { //DO some db writing db.setTransactionSuccessful(); }catch(Exception e){ // here you can catch all the exceptions e.printStack(); } finally { db.endTransaction(); } 

examples for catch catch

http://www.java-samples.com/showtutorial.php?tutorialid=293

http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html

+6
source

For me, using e.printStackTrace(); worked instead of e.printStack();

 SQLiteHelper dbHelper = new SQLiteHelper(mContext); mDatabase = dbHelper.getReadableDatabase(); mDatabase.beginTransaction(); try { }catch(Exception e){ // here you can catch all the exceptions e.printStackTrace(); } finally { mDatabase.endTransaction(); } mDatabase.close(); 
0
source

Source: https://habr.com/ru/post/1433818/


All Articles