Monday, July 23, 2018

java - Breaking 2 nested loops "at once"

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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...