One approach is to use an exception, depending on the expected result of the db.insert routine.
In short, create a new EDBInsertionException exception (a class extending Exception) and raise the exception in your for loop.
try
{
while((str=br.readLine())!=null)
{
int tempCounter=0;
for(int i=0;i {
String word = str.substring(i, i+3);
// System.out.println(word);
int insert = db.insert(index, word);
if(insert<0)
{
throw new EDBInsertionException("No record inserted: " + insert);
}
tempCounter++;
}
index++;
System.out.println("Index Updated: "+index);
System.out.println(String.valueOf("Total words in this run: "+tempCounter));
if(index==5)
{
break;
}
}
}
catch (EDBInsertionException e)
{
// MZ: The database insertion has failed
log.error(e.getMessage(), e);
// MZ: Supplemental action: Delegate, escalate, rollback, etc
}
EDIT
Expanded the example to illustrate a potential usage of an (user) exception.
No comments:
Post a Comment