Sumeet Vishwas
Sumeet Vishwas

Reputation: 593

throw exception without breaking loop in java

I need to handle custom exception in java inside loop so i am throwing new custom exception but it breaks the loop. How we can manage to throw loop without breaking loop. Here is my code

    for (int i = 0; i < j; i++) {
        File currFile = arrayOfFile1[i];
        if (currFile.isFile()) {
            if (currFile.exists()) {
                try {
                    String fileName = currFile.getName();
                    if (fileName.startsWith("~$")) {
                        continue;
                    } else {
                        if ((fileName.endsWith(".xlsx")) || (fileName.endsWith(".xls"))) {
                            if (fileName.endsWith("_arch.xlsx") || fileName.endsWith("_arch.xls")) {
                                continue;
                            }
                            try {
                                IDSOutputGenerator.generateOutput(currFile.getAbsolutePath(), "", outputType,
                                        false);
                            } catch (CheckErrorHandle e) {
                                throw new CheckErrorHandle();
                            }
                        }
                        else if (fileName.endsWith(".xml")) {
                            try {
                                IDSOutputGenerator.generateOutput(currFile.getAbsolutePath(), "", outputType,
                                        false);
                            } catch (CheckErrorHandle e) {
                                throw new CheckErrorHandle();
                            }

                        } else {
                            throw new CheckErrorHandle(currFile.getAbsolutePath(), "File type not supported");
                        }
                    }
                } catch (CheckErrorHandle e) {
                    throw new CheckErrorHandle();
                } catch (Exception e) {
                    throw new CheckErrorHandle("", e.getMessage());
                }
            } else {
                throw new CheckErrorHandle(currFile.getAbsolutePath(), "File does not exist");
            }
        }
    }

Upvotes: 5

Views: 17848

Answers (2)

NickJ
NickJ

Reputation: 9579

One possibility - move the entire body of the loop into another method which handles the exceptions without re-throwing:

e.g.

for (int i = 0; i < j; i++) {
    File currFile = arrayOfFile1[i]; 
    handlefile(currFile);
}

and the method:

private void handleFile(File file) {
  //body of loop in here
  //log exceptions, don't re-throw
}

Another possibility is to havethe above method return a boolean success flag:

Loop:

for (int i = 0; i < j; i++) {
    File currFile = arrayOfFile1[i]; 
    if (handlefile(currFile)) {
       //do something on success
    } else {
       //do something on failure
    }
}

Method:

private boolean handleFile(File file) {
  try {
    //do stuff
    return true;
  } catch (Exception e) { //or more specific exceptions/multi-catch
     //log exception
     return false;
  }
}

Upvotes: 1

saroff
saroff

Reputation: 698

Something like:

Exception ex = null;
for (;;) {
    ex = new Exception();
}
if (ex != null) throw ex;

Or, as comment suggesting:

List<Exception> errors = new ArrayList<>();
for (;;) {
    errors.add(new Exception());
}
if (!errors.isEmpty()) {
    //Do something with errors
}

General idea is that only thing that you can do - store exception somewhere before throwing it. If you throwing exception - then only way to avoid breaking loop is to catch it immediately.

Upvotes: 7

Related Questions