[Help]How to handle exception from dirEntries?

Keqin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 16 21:44:42 PDT 2014


Hi everyone,

I have write a short program to check file size recursively as 
bellow.

If i run this exe on windows eg.:
> hello.exe --dir C:\

It will throw as:

std.file.FileException at std\file.d(2519): 
C:\$Recycle.Bin\S-1-5-18: Access is den
ied.

I believe the exception comes from the following line:
auto entries = dirEntries(dirpath, SpanMode.depth);

I have read the std.file, still have no idea how to get around of 
it.
Can someone help?

..code-block: d

import std.stdio;
import std.file;
import std.getopt;
string dirpath=".";

void main(string[] args) {
     getopt(args, "dir", &dirpath);
     writeln(dirpath);
     auto entries = dirEntries(dirpath, SpanMode.depth);
     writeln("start while");
     while(!entries.empty)
     {
         try{
             auto entry = entries.front;
             if(entry.isFile())
             {
                 writeln(getSize(entry));
             }
             entries.popFront();
         }
         catch(FileException o){
             //handle FileException
         }
         catch(Exception o){
             //handle all other types of exceptions
         }
         finally{
             //do nothing
         }
     }
}


More information about the Digitalmars-d-learn mailing list