dirEntries() and exceptions

rjframe dlang at ryanjframe.com
Fri Nov 24 14:04:15 UTC 2017


On Fri, 24 Nov 2017 12:02:47 +0000, doc wrote:

> I'm trying recursively find files, and have some trouble to catch
> exceptions if have no permission to read directory.
> 
...
> 
> std.file.FileException at std/file.d(3798):
> /tmp/systemd-private-8338348a306b4d589e3f6ba2bfd0c8fe-systemd-
timesyncd.service-3374MK:
> Permission denied ----------------
> 
> [code]
> void main() {
> import std.stdio, std.file;
> 
> try {
> auto farray = dirEntries("/tmp", "*.{d,py,ph,sh}",
> SpanMode.breadth);
> foreach (f; farray) {
> writeln(f);
> }
> catch (FileException e) {
> writeln(e.msg);
> }
> }
> [/code]
> 
> This not work, after writeln(e.msg); program is exit.

The exception for dirEntries was reported as a bug here[1]; there is a 
workaround listed on that page that silently skips directories it can't 
read, but you'll need to modify it a bit to maintain a breadth search.

As to why your code stops after catching the exception, the try/catch 
block does not place you back in the block that threw the exception, but 
drops you off in the outer scope. You would need to place the code in some 
sort of loop to run until all input was consumed in order continue past 
the exception.


[1]: https://issues.dlang.org/show_bug.cgi?id=12391


More information about the Digitalmars-d-learn mailing list