If I catch an exception during execution of a foreach, can I continue execution?
foreach (string d in Directory.GetDirectories(path))
{
try
{
foreach (string f in Directory.GetFiles(path))
{
//do something
}
//do something
}
catch
{
// If there is an error in the foreach, I want it to keep on going
}
}
I am asking this because it suddenly terminates before the foreach
gets all the files.
Simply do:
foreach (string d in Directory.GetDirectories(path))
{
foreach (string f in Directory.GetFiles(path))
{
try
{
//do some thing
}
catch
{
// If there is an error on the foreach, I want it to keep on going to search another one
}
}
//do some thing
}
foreach (string d in Directory.GetDirectories(path)) {
foreach (string f in Directory.GetFiles(path)) {
try {
//do some thing
} catch { /* log an error */ }
}
try {
//do some thing
} catch { /* log an error */ }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With