I have the following two methods
Method 1
public String[] getSongIds(String whereClause)
{
String countQuery = "SELECT songid FROM TABLE_INDEX WHERE " + whereClause;
Cursor cursor = db.rawQuery(countQuery, null);
int cursorSize = cursor.getCount();
int[] songIds = new int[cursorSize];
int count=0;
if (cursor != null ) {
if (cursor.moveToFirst()){
do{
songIds[count] = cursor.getInt(cursor.getColumnIndex("songid"));
count++;
}while(cursor.moveToNext());
}
}
cursor.close();
db.close();
return getSongTitles(songIds);
}
Method 2
private String[] getSongTitles(int[] songIds) {
/some algorithm
return songTitles;
}
Method 1 is called from a different package. Method 1 runs a query against SQLite database and calls the 2nd method. I need to catch the exception often cause by executing the SQLite query in method 1. Preferably returning (-1) or something so I can display a message to user from the package where these methods were initially called. So I want method 1 to avoid calling method 2 if there was a (wrong input) SQL exception, instead return something back to the other package
p.s I saw couple ways to catch this exception but weren’t satisfied with their approach. Want to know whats the best way to deal with this. cheers
Catch the exception, wrap it in a custom one, and throw it:
Then, whoever calls
getSongIdsneeds to catch this new exception: