Return from and Exception Question

  • Thread starter Thread starter needin4mation
  • Start date Start date
N

needin4mation

I know there is no On Error or the like in C#. But I am not sure how
to make this code work when the file it tries to copy is in use, not
there, etc. Here is what I have tried and when the exception is thrown
the listbox updates, but the program just dies. I want it to go to the
next file in my list. Thank you for any help:

private void btnCopyFiles_Click(object sender, System.EventArgs e)
{
try
{
foreach (ListViewItem item in listViewFiles.Items)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = 4; //filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file
being copied.
pBar1.Step = 1;

string source=item.SubItems[0].Text.Trim();
string destination=item.SubItems[1].Text.Trim();
lisInfoList.Items.Add("Copying: " + source + " to " + destination);
File.Copy(source,destination,true);
item.SubItems[2].Text="Success";
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
Application.DoEvents();


}
}
catch (Exception exc)
{
lisInfoList.Items.Add("Error Copying: " + exc.Message + ", " +
exc.Source);
return;
}
 
private void btnCopyFiles_Click(object sender, System.EventArgs e)
{
foreach (ListViewItem item in listViewFiles.Items)
{
try
{

// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = 4; //filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file being
copied.
pBar1.Step = 1;

string source=item.SubItems[0].Text.Trim();
string destination=item.SubItems[1].Text.Trim();
lisInfoList.Items.Add("Copying: " + source + " to " + destination);
File.Copy(source,destination,true);
item.SubItems[2].Text="Success";
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
Application.DoEvents();
}
catch (Exception exc)
{
lisInfoList.Items.Add("Error Copying: " + exc.Message + ", " +
exc.Source);
}
}
}
 
I would just have a catch block that sets a flag (boolean) to true if an
exception is thrown. Then, in a while loop, continue until there is no
exception.

Of course, this isn't really a good idea. You should probably have some
higher-level logic to do this, since just looping over and over could cause
your program to hang for an incredibly long time.

Hope this helps.
 
If the current file fails to copy you want the app to try the next correct?
Just put the try catch in the loop instead of having the loop in the try
catch?
 
So like:

done=false;
while (!done)
{
try
{copy stuff}
catch
{error stuff update my listbox}
finally{done=true;}
}

Is that what you mean? But if I do that will it go to the next item in
my foreach statement or try and do all collection again? And thank you.
 
I'm not sure what you mean. I can't figure out where to put my flag to
terminate the loop. If I hit the exception, it will just go back to
the top of the while loop and start at the first file again, not the
next file in the foreach statement (I think):

bool done = false;
while (!done)
{
try
{
btnCopyFiles.Enabled=false;
foreach (ListViewItem item in listViewFiles.Items)
{

string source=item.SubItems[0].Text.Trim();
string destination=item.SubItems[1].Text.Trim();
lisInfoList.Items.Add("Copying: " + source + " to " + destination);
File.Copy(source,destination,true);
item.SubItems[2].Text="Success";
}

}
catch (Exception exc)
{
lisInfoList.Items.Add("Error Copying: " + exc.Message + ", " +
exc.Source);

}

finally
{
lisInfoList.Items.Add("Completed.");
btnCopyFiles.Enabled=true;

}
}
 
I'm not sure what you mean. I can't figure out where to put my flag to
terminate the loop. If I hit the exception, it will just go back to
the top of the while loop and start at the first file again, not the
next file in the foreach statement (I think):

You've missed carion1's meaning. You've got the try/catch *outside* the
foreach loop instead of inside it. If you put it inside the loop, then
it can just go to the next iteration of the loop.
 
Back
Top