Исключение SQLiteConstraintException не обнаружено

В чем проблема с этим кодом? Он не перехватывает исключение, генерируемое методом insertChild () .

childDbOps.open();
try {
    childDbOps.insertChild(child);
} catch (SQLiteException exception) {
    Log.i("error la inserare child", "on the next line");
    exception.printStackTrace();
} finally {
    childDbOps.close();
}

Ошибка:

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) 

Это Android-sqlite. Строка - это когда вызывается метод вставки.

9
задан dira 1 October 2012 в 04:30
поделиться

2 ответа

Вы ловите только исключения типа SQLiteException. Если метод insertChild выдает любое другое исключение, он не будет пойман.

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();
}
3
ответ дан 4 December 2019 в 06:11
поделиться

@bogdan есть ли другое место, где вы вызываете insertChild (child); кроме этого места. вы поместили трассировку в блок try, чтобы узнать, доходит ли она до этого блока, и распечатать трассировку, как показано ниже.

попробуйте {Log.i ("приходит здесь ");
childDbOps.insertChild (дочерний элемент); }

дайте мне знать.

0
ответ дан 4 December 2019 в 06:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: