FileException Inconsistency?
Alex
alexander.edwards at gmail.com
Mon May 11 04:18:33 UTC 2026
Hi,
After too many years of thinking to try D, I'm creating a simple
directory scanner. However, I'm already getting some
"inconistencies" in Exception handling I'm confused by and trying
to debug.
I'm using Visual D on Windows. DMD 2.110
In the code below, if I leave the writeln(entry.name) or even
just writeln(".") uncommented, then I get a FileException for
permission-denied directories as expected.
If I comment these lines out, e.g. to add the entry.name to an
array, instead I get a Throwable std.file.FileException which is
not caught as a FileException or Exception.
Is this expected? How to best handle this?
Thanks
```
module DirScan;
import std.stdio;
import std.file;
import std.algorithm;
import std.array;
import core.thread;
import std.compiler;
int main()
{
writeln("DirScan\n");
writefln("Compiler: %s v%d.%03d", name, version_major,
version_minor);
string path = "C:\\";
scan(path);
writeln("End. Press Enter.");
readln();
return 0;
}
void scan(string path)
{
writeln("-----");
writeln("PATH: ", path);
foreach (DirEntry entry; dirEntries(path, SpanMode.shallow))
{
try
{
writeln(entry.name);
writeln(".");
if (entry.isDir)
scan(entry.name);
}
catch (FileException fe) {
writeln("File Exception: ", fe);
readln();
continue;
}
catch (Exception e) {
writeln("Exception: ", e);
readln();
continue;
}
catch (Throwable t) {
writeln("Throwable: ", t);
readln();
return;
}
}
}
```
More information about the Digitalmars-d-learn
mailing list