The Artima Developer Community
Sponsored Link

The C++ Source
Sidebar - Taking Exception to Error Handling

Modern C++ practitioners always write exception-safe code. Failure to do so is simply to write invalid code. Furthermore, there is now an assumption that C++ is always used in contexts in which exceptions are supported, which entails bringing in some heavy support from the C/C++ runtime libraries. Sometimes, this is not the case, either because you need to have a very small executable module for internet download, or because the operating environment does not support them, or has size constraints, etc.

Whatever the reason, it's good to write libraries that can work with or without exception handling; I tend to do this whenever it's possible and not inordinately inconvenient. If you look at the actual implementation file for the glob_sequence class (globsequencemethods.h), you will see that the code is written to behave robustly and meaningfully in the context where exceptions are not supported. In that case, failures of glob() result in an empty glob_sequence instance being constructed. Whether you use begin()/end() or size() with the subscript operator, your code will work correctly.

There's another exception issue, in so far as what to do when stat() returns an error code. In this case, I chose not to implement it to throw exceptions, for two reasons. First, file-system entries may be deleted or have their access permissions changed asynchronous [14] to the execution of a given process that may be enumerating them. I think in this case it's appropriate that glob_sequence show what's accessible "now", rather than at the epoch of the call to glob(). After all, if a file has been deleted/moved in that short time, it's more likely to stay that way than come back in time for the client code of glob_sequence to use it.

Second, there are some inconsistencies between operating environments in terms of which error codes might be used by stat(), since they may well have different underlying implementations, and also in terms of which error codes are actually defined on a given system. I chose to give glob_sequence behaviour that was generally compilable, and didn't exhibit different behaviour depending on the system on which it was compiled and/or run. Hence, if stat() fails, the entry is removed from the list. End of story.

Sponsored Links

Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us