Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch exception in sqllite transaction - java android

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

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

my understanding is that this will automatically rollback the data should the transaction not be set to successful as it is in the try, but how do i catch if it was not successful so i can notify the user on success or rollback

like image 563
erik Avatar asked Sep 12 '12 13:09

erik


1 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 try catch

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

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

like image 69
Pratik Avatar answered Nov 14 '22 00:11

Pratik