473,805 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return from and Exception Question

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_Cl ick(object sender, System.EventArg s e)
{
try
{
foreach (ListViewItem item in listViewFiles.I tems)
{
// 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.Lengt h;
// 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.Sub Items[0].Text.Trim();
string destination=ite m.SubItems[1].Text.Trim();
lisInfoList.Ite ms.Add("Copying : " + source + " to " + destination);
File.Copy(sourc e,destination,t rue);
item.SubItems[2].Text="Success" ;
// Perform the increment on the ProgressBar.
pBar1.PerformSt ep();
Application.DoE vents();
}
}
catch (Exception exc)
{
lisInfoList.Ite ms.Add("Error Copying: " + exc.Message + ", " +
exc.Source);
return;
}

Nov 17 '05 #1
7 1836
private void btnCopyFiles_Cl ick(object sender, System.EventArg s e)
{
foreach (ListViewItem item in listViewFiles.I tems)
{
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.Lengt h;
// 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.Sub Items[0].Text.Trim();
string destination=ite m.SubItems[1].Text.Trim();
lisInfoList.Ite ms.Add("Copying : " + source + " to " + destination);
File.Copy(sourc e,destination,t rue);
item.SubItems[2].Text="Success" ;
// Perform the increment on the ProgressBar.
pBar1.PerformSt ep();
Application.DoE vents();
}
catch (Exception exc)
{
lisInfoList.Ite ms.Add("Error Copying: " + exc.Message + ", " +
exc.Source);
}
}
}

--

Derek Davis
dd******@gmail. com
<ne***********@ gmail.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
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_Cl ick(object sender, System.EventArg s e)
{
try
{
foreach (ListViewItem item in listViewFiles.I tems)
{
// 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.Lengt h;
// 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.Sub Items[0].Text.Trim();
string destination=ite m.SubItems[1].Text.Trim();
lisInfoList.Ite ms.Add("Copying : " + source + " to " + destination);
File.Copy(sourc e,destination,t rue);
item.SubItems[2].Text="Success" ;
// Perform the increment on the ProgressBar.
pBar1.PerformSt ep();
Application.DoE vents();
}
}
catch (Exception exc)
{
lisInfoList.Ite ms.Add("Error Copying: " + exc.Message + ", " +
exc.Source);
return;
}

Nov 17 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<ne***********@ gmail.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
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_Cl ick(object sender, System.EventArg s e)
{
try
{
foreach (ListViewItem item in listViewFiles.I tems)
{
// 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.Lengt h;
// 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.Sub Items[0].Text.Trim();
string destination=ite m.SubItems[1].Text.Trim();
lisInfoList.Ite ms.Add("Copying : " + source + " to " + destination);
File.Copy(sourc e,destination,t rue);
item.SubItems[2].Text="Success" ;
// Perform the increment on the ProgressBar.
pBar1.PerformSt ep();
Application.DoE vents();
}
}
catch (Exception exc)
{
lisInfoList.Ite ms.Add("Error Copying: " + exc.Message + ", " +
exc.Source);
return;
}

Nov 17 '05 #3
How about a nested try?

Nov 17 '05 #4
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?

--

Derek Davis
dd******@gmail. com
<ne***********@ gmail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
How about a nested try?

Nov 17 '05 #5
So like:

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

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.

Nov 17 '05 #6
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.En abled=false;
foreach (ListViewItem item in listViewFiles.I tems)
{

string source=item.Sub Items[0].Text.Trim();
string destination=ite m.SubItems[1].Text.Trim();
lisInfoList.Ite ms.Add("Copying : " + source + " to " + destination);
File.Copy(sourc e,destination,t rue);
item.SubItems[2].Text="Success" ;
}

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

}

finally
{
lisInfoList.Ite ms.Add("Complet ed.");
btnCopyFiles.En abled=true;

}
}

Nov 17 '05 #7
<ne***********@ gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3129
by: Victor Hannak | last post by:
I have a class with the following public function: float FuncClass::GetResult() { try { return(Result); } catch (...) { WriteString("FuncClass::GetResult ERROR: Exception encountered"); return(0); } }
11
2890
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses return code (not generating error/exception) so it is more compatible with other programming language.
94
13914
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it would be more orthogonal if a method could only have one return statement. --
8
1862
by: Daniel Billingsley | last post by:
Suppose I have a method that returns some type of object, and in that method I have a try...catch block and just throw my own exception when I catch one. The compiler insists that all code paths return a value, so... do I just put return null; after I throw my exception in the catch block? or should I initialize the local variable to null and return it in a finally block?
40
3165
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false. My current approach is to have...
1
1131
by: JIM.H. | last post by:
Hello, I am trying to learn web service implementation, I have a question about return value. I will be returning and xml string with many records if everything is fine and the reader reads it and puts it into a data set. However if the there is some kind of problem, how should I return error in web service and how do reader in the other side can catch it and deal with it. It would be very helpful if I can get some sample codes. Thank you.
24
2202
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from the UI layer to a method in a library class that calls the stored procedure. Best to return a boolean indicating success/failure, return a string with the exception message, or just return the entire exception?
2
1680
by: ELINTPimp | last post by:
Hello all, Have a really interesting problem (at least to me) with my upload_file() function. I have it working now, with a bit of a work around, but would like to know what everyone thinks in regards to this being a bug or perhaps my ignorance. Basically the function takes two arguments, one is the desired file path I want the upload file to be moved to, and the second is an arbitrary integer. If the file doesn't already exist, the...
12
1481
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if (hashString == "MD5") { return System.Security.Cryptography.MD5.Create(); } else { return System.Security.Cryptography.SHA512.Create(); }
18
2658
by: Carl Forsman | last post by:
there are 2 ways to return a value from a function ==================== 1) passing a reference as parameter - the following will return time void Table::Get(char* FieldName, *SYSTEMTIME time) { _variant_t vtValue; vtValue = m_Rec->Fields->GetItem(FieldName)->GetValue(); if (vtValue.vt == VT_NULL) {
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.