Reputation: 2146
Am just wondering how to workaround my scenario,here is my code
try
{
bool b;
foreach (string file in files)
{
#region donloadfiles
if (b = file.Contains(story))
{
try
{
logger.Info("calling xml creation Method");
baseMeta(story, XML);
logger.Info("XML created");
}
catch (Exception ex)
{ logger.Error(ex.Message); throw; }
logger.Info("calling Download Method");
Download(file, story, xml, program);
logger.Info("Download Method processed successfully");
}
#endregion
}
}
catch (Exception ex)
{ logger.Error(ex.Message); throw; }
As promised,here is my main method contains try catch block
try
{
//update the status here in DB
Status = "Closed";
Update(status);
}
catch (Exception ex)
{
Status = "Failed";
Update(status);
break;
}
i have directory "for eg:C:\" my getlist method grab all the record and pass it to my foreach loop "foreach (string file in files)" then i have a condition
if (b = file.Contains(story)) to check any of my file have the name "story" then do some logic inside.this thing works good.
Now what am trying to do is,if none of the files are matching then i have to forcefully call the catch and throw to the main catch,am doing some logic update in my main catch.
Someone please advice me how can i workaround this scenario.
Thanks in advance
Upvotes: 1
Views: 2725
Reputation: 5083
Usher, using exceptions to manage process flow is a BAD idea! Exceptions are there to manage errors, and not to handle expected conditions in your code's execution.
A much better way (in the long run:trust me on this) would be to return some value out of your method when none of the files match instead of throwing an exception.
Something like:
#region donloadfiles
if (b = file.Contains(story))
{
try
{
logger.Info("calling xml creation Method");
baseMeta(story, XML);
logger.Info("XML created");
}
catch (Exception ex)
{ logger.Error(ex.Message); throw; }
logger.Info("calling Download Method");
Download(file, story, xml, program);
logger.Info("Download Method processed successfully");
}
else return "no matches found";
#endregion
and handle the process from there once you've gotten the "no matches found" value in the place that called this method.
Upvotes: 1
Reputation: 30107
what am trying to do is,if none of the files are matching then i have to forcefully call the catch and throw to the main catch,am doing some logic update in my main catch.
bool anyMatch = false;
foreach (.....)
{
if (b = file.Contains(story))
anyMatch = true;
}
if(!anyMatch)
throw ... //this will be caught by main catch
Keep a bool variable outside foreach loop. Set it to true if any file matches. if it is false at the end of foreach loop throw exception
Upvotes: 3