[phobos] Calling abort() on unhandled exception
Andrei Alexandrescu
andrei at erdani.com
Fri Jul 30 16:17:29 PDT 2010
Steve Schveighoffer wrote:
> What's hard about using something other than exceptions to print usage in short
> scripts?
>
> This looks just as readable/easy to me:
>
> import std.usage;
>
> int main(string[] args)
> {
> checkUsage(args.length > 1, "Usage: prog args");
> ....
> }
>
> I also would like to see stack traces in small scripts too, esp. when the script
> isn't throwing the exception.
Here's one example I used:
#!/usr/bin/env rdmd
import std.exception, std.file;
void main(string[] args) {
enforce(args.length == 3,
"Usage: trcopy file1 file2");
auto tmp = args[2] ~ ".messedup";
scope(failure) if (exists(tmp)) remove(tmp);
copy(args[1], tmp);
rename(tmp, args[2]);
}
It could end with at least a couple distinct exception types. Clearly
we're not cutting the pie the right way. The right solution is - absent
any handler, an exception leaking out of main() prints the message and
exit(1).
Andrei
More information about the phobos
mailing list