5 Replies - 607 Views - Last Post: 06 May 2016 - 01:01 PM Rate Topic: -----

#1 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

How do I make my code skip if it hits an error

Posted 06 May 2016 - 09:52 AM

How do I make it so when my code hits an error which it does right now, it skips that file and starts with the next one, let me demo.

        private void Clean_Click(object sender, EventArgs e)
        {

            

            string tempPath = Path.GetTempPath();
            DirectoryInfo di = new DirectoryInfo(tempPath);
            

            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true);
            }

            foreach (FileInfo file in di.GetFiles())
            {
                try
                {
                    file.Delete();
                }
                catch (Exception)
                {
                    // Log error.
                }
            }

        }



What it does now is it deleted files and folders in my temp folder but I want to make it so when it hits an error, when its not allowed to delete a file or a file is in use I dont want it to display an error I want it to skip that file and move on to the next one, what type of loop would I use for this ?

Is This A Good Question/Topic? 0
  • +

Replies To: How do I make my code skip if it hits an error

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How do I make my code skip if it hits an error

Posted 06 May 2016 - 10:23 AM

Any type of loop you want.

What you need is to understand how to use the keyword continue.

You already have a try/catch. That's good. You put in a comment that says "log exception". Well, if you just want to skip the record that causes an Exception to be thrown, all you have to do is write your catch like this:

catch 
{
    continue;
}


That causes the loop to immediately move to its next iteration (or end, assuming there's no more items left to iterate).

Of course, it's better not to "swallow errors".

catch (Exception exc) 
{
    logger.Error(exc); //example requires NLog, but you can log however you want;
    continue;
}

Was This Post Helpful? 0
  • +
  • -

#3 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: How do I make my code skip if it hits an error

Posted 06 May 2016 - 10:34 AM

Oh! So its that simple! Thank you so much, and yeah, logging the error would be very nice, never heard about NLog I should probably look into that! Thanks! :D/>

Oh nevermind it didnt fix it! Its still throwing me the error and its not bypassing it.

Posted Image

Nevermind I fixed it! I had to add the same lines of code to the dir deleter aswell!

This post has been edited by andrewsw: 06 May 2016 - 10:36 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: How do I make my code skip if it hits an error

Posted 06 May 2016 - 10:45 AM

Okay so.. Here is the deal not sure why its acting this way, or why its not looping atleast.

Whats happening is that it deletes 1 folder and then it hits a folder it cant delete, it doesnt skip that and jump to the next one it just stops it doesnt loop through and find the next one it just stops. (Changed the code a bit)
Any idea why its not looping?



        private void Clear_Click(object sender, EventArgs e)
        {

            

            string tempPath = Path.GetTempPath();
            DirectoryInfo di = new DirectoryInfo(tempPath);
            

            foreach (DirectoryInfo dir in di.GetDirectories())
            {


                try
                {
                    dir.Delete();
                }
                catch (Exception)
                {
                    // Log error.
                    continue;
                }

               // dir.Delete(true);
            }




            foreach (FileInfo file in di.GetFiles())
            {
                try
                {
                    file.Delete();
                }
                catch (Exception)
                {
                    // Log error.
                    continue;
                }
            }

        }

Was This Post Helpful? 0
  • +
  • -

#5 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: How do I make my code skip if it hits an error

Posted 06 May 2016 - 10:54 AM

Never mind! I had to change
dir.Delete();

to


dir.Delete(true);
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How do I make my code skip if it hits an error

Posted 06 May 2016 - 01:01 PM

You should look up what those parameters actually do.

https://msdn.microso...(v=vs.110).aspx

That particular parameter tells the DirectoryInfo object to recursively delete the folder. What that means is that it will delete all the contents inside the folder, then delete the folder itself. If any of the contents are directories themselves, the same thing will be done to them.

In fact, if you're using that overload, you don't have to manually delete the files, since that will delete the files in the folder for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1