views:

288

answers:

2
+1  Q: 

try catch problem

Hello,

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

+4  A: 

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();
}
Amarghosh
I also tried with Exception which I know that is the superclass of all the exceptions and still doesnt work
bogdan
@bogdan Are you sure there is an exception - what is the exception thrown? Is it thrown by `childDbOps.open();`, which is outside try block?
Amarghosh
SQLiteConstraintException and is because I test what happens with my application when I enter again data with same primary key and I want to handle this. The insert method is my problem
bogdan
But I dont know why It is not caught
bogdan
[SQLiteConstraintException inherits SQLiteException](http://developer.android.com/reference/android/database/sqlite/SQLiteConstraintException.html) - it should be caught; insert a `catch(SQLiteConstraintException sqlEx){}` as the first catch block to confirm.
Amarghosh
still doesn't workthis is a part of the exception log and that is line 203 ->08-06 07:21:16.148: ERROR/Database(320): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failedat 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)
bogdan
A: 

@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.

Suresh S
I put the log it enters right there. thats the only place I call the method
bogdan
@bogdan thanks for the reply.from the trace i can see that android.view.View.performClick method handles the exception (means it has the catch block) , before it comes to your catch
Suresh S
@bogdan did u solve the issue?
Suresh S
hello, I solved my issue but without using the try catch. I make another query into the database to check if that primary key exists and that's it. I wasn't able to solve it in this form
bogdan