Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7789681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:22:18+00:00 2026-06-01T21:22:18+00:00

I have a method being called by another class that performs a number of

  • 0

I have a method being called by another class that performs a number of steps on a collection of objects. Specifically, I’m copying a list of files from their original locations to a new folder. After the files have been copied, I update a database with additional data related to the group of files. It looks something like this:

public void copyFilesAndStore(File[] files, DataObject additionalData){
    for (File f in files){
        copyFileToDestination(f);
    }
    updateDatabase(additionalData);
}

The issue is, that the copyFileToDestination(File f) method can throw various IOExceptions if there’s an issue with one of the files, but even if there is an exception on one of the files, I’d like to continue copying the rest of the files, and make the call to update the database. So I can do something like this:

public void copyFilesAndStore(File[] files, DataObject additionalData){
    for (File f in files){
        try {
            copyFileToDestination(f);
        } catch (IOException ex) {
            log(ex);
        }
    }
    updateDatabase(additionalData);
}

However, the class calling copyFilesAndStore() needs to know if the copy was successful or not, but if I catch all the exceptions inside this method, it won’t know about them. Does it make sense at all to do something like below, or am I missing a better solution?

public void copyFilesAndStore(File[] files, DataObject additionalData){

    IOException encounteredException = null;

    for (File f in files){
        try {
            copyFileToDestination(f);
        } catch (IOException ex) {
            log(ex);
            encounteredException = ex;
        }
    }

    updateDatabase(additionalData);

    if (encounteredException != null)
        throw(encounteredException);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:22:21+00:00Added an answer on June 1, 2026 at 9:22 pm

    It is perfectly safe to store an exception (excluding some corner cases, like OutOfMemoryException and the like), but if you want to store possible exceptions in a loop, it is usually a good idea to store them all:

    public void copyFilesAndStore(File[] files, DataObject additionalData){ 
    
        List<IOException> encounteredExceptions = new LinkedList<IOException>(); 
    
        for (File f in files){ 
            try { 
                copyFileToDestination(f); 
            } catch (IOException ex) { 
                log(ex); 
                encounteredExceptions.add(ex); 
            } 
        } 
    
        updateDatabase(additionalData); 
    
        if (!encounteredExceptions.empty()) {
            throw(new DelayedException(encounteredExceptions)); 
        }
    } 
    

    where DelayedException is your own custom exception that can store a list of other exceptions (kind of like the caused-by chain in a regular exception)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that requires a specific method to be called before being
I have a method that takes a list of entities ( Class es) and
I have an abstract class called DatabaseRow that, after being derived and constructed, is
I have this classA that has a method in it, that is being called
I have got a textbox on a form with a method being called from
I have an exception being thrown from a C# method, that takes a generic
I have method in a class that I need to make sure is only
I have a method called action() that deploys three threads. Each deployed thread or
I have a fairly process intensive method that takes a given collection, copies the
I have a method that creates popups on tap, called onBalloonTap and i am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.