Reputation: 131
What is the problem with this code? It doesn't catch the exception thrown by insertChild()
method.
childDbOps.open();
try {
childDbOps.insertChild(child);
} catch (SQLiteException exception) {
Log.i("error la inserare child", "on the next line");
exception.printStackTrace();
} finally {
childDbOps.close();
}
The error is:
ERROR/Database(320): android.database.sqlite.SQLiteConstraintException: error code 19:
constraint failed at com.android.dataLayer.DbAdapter.insertChild(DbAdapter.java:169)
at com.android.activities.ChildInsertActivity.onClick(ChildInsertActivity.java:203)
at android.view.View.performClick(View.java:2344)
It is android sqlite. The line is when the insert method is called.
Upvotes: 12
Views: 14105
Reputation: 761
The SQLiteDatabase.insert() method is used in the cases where you want to handle database writes without unwinding the stack if a write fails. If you want to catch exceptions when inserting into the database, use the SQLite.insertOrThrow() method. It will throw an exception which you can then catch and handle.
Upvotes: 45
Reputation: 12585
@bogdan is there any other place u are calling insertChild(child); other than this place. did you put a trace in try block to know whether it comes to this block and print the trace like below.
try { Log.i("comes here");
childDbOps.insertChild(child); }
let me know.
Upvotes: 0
Reputation: 59461
You're catching only exceptions of type SQLiteException
. If the insertChild
method throws any other exception, it won't be caught.
try {
childDbOps.insertChild(child);
}
catch(SQLiteException exception) {
Log.i("error la inserare child", "on the next line");
exception.printStackTrace();
}
catch(AnotherException exception) {
//handle it
}
//Catch them all here
catch(Exception exception) {
//handle it: must handle it, don't just swallow it.
}
finally {
childDbOps.close();
}
Upvotes: 3