Continue iteration after exception

monarch_dodra monarchdodra at gmail.com
Wed Jan 23 08:45:15 PST 2013


On Wednesday, 23 January 2013 at 16:10:57 UTC, H. S. Teoh wrote:
> On Wed, Jan 23, 2013 at 04:52:25PM +0100, monarch_dodra wrote:
>> On Wednesday, 23 January 2013 at 15:24:02 UTC, Josh wrote:
>> >On Wednesday, 23 January 2013 at 15:00:16 UTC, bearophile 
>> >wrote:
>> >>A possible solution: desugar the foreach range iteration
>> >>protocol of dirEntries and wrap the relevant method with a
>> >>try-catch.
>> >
>> >Sorry, could you explain that a little? I'm not sure what 
>> >desugar
>> >means :/
>> 
>> He means you turn it into a normal loop with !empty, front and
>> popFront.
>
> [SNIP]
>
>> That wouldn't really work anyways. Why you'd be able to catch 
>> the
>> exception when calling popFront, and preserve your iteration 
>> state,
>> you'd be unable to iterate past the exception point :/
>
> Yeah, I think we might need an enhancement request to add a 
> flag to
> ignore access errors while traversing the filesystem.
>
>
> T

As a workaround, one can manually and recursively iterate the 
files with a "shallow" span. The result would be a depth first 
iteration.

//----
void foo(string s)
{
     writeln(s);
     if (!s.isDir()) return;
     try
     {
         foreach(string dir; dirEntries(s, SpanMode.shallow))
         {
             foo(dir);
         }
     }
     catch {}
}

void main()
{
     foo(`C:\\`);
}
//----


More information about the Digitalmars-d-learn mailing list