is collectException working for every exceptions ?

Andre Pany andre at s-e-a-p.de
Mon Mar 18 21:23:42 UTC 2019


On Monday, 18 March 2019 at 18:54:22 UTC, Roman Sztergbaum wrote:
> Hello as the subject say i'm asking this question because with 
> the following code
>
> ```
> private config_create_answer create_config(string[] args)
>     in
>     {
>         assert(args !is null, "args cannot be null");
>         assert(args.length == 2, "need 1 arguments");
>     }
>     out (r)
>     {
>         assert(r.state == "SUCCESS", "create_config should 
> success");
>         assert(!r.config_key.empty, "config_key should not be 
> empty");
>         assert(!r.readonly_config_key.empty, 
> "readonly_config_key should not be empty");
>     }
>     body
>     {
>         string config_name;
>         getopt(args, "name", &config_name);
>         auto cfg = (cast(const 
> char[])(std.file.read("../API_doc/json_recipes/config_create.json")))
>             .deserialize!config_create;
>         cfg.config_name = config_name.strip("\"");
>         client_.socket.send(cfg.serializeToJson);
>         auto answer = new ubyte[256];
>         client_.socket.receive(answer);
>         client_.socket.getErrorText.writeln;
>         return (cast(string) 
> answer).deserialize!config_create_answer;
>     }
>
>     unittest
>     {
>         import std.exception : collectException;
>
>         auto cli = new 
> CLI("/tmp/raven-os_service_albinos.sock");
>         assert(cli.create_config(["create_config", 
> "--name=toto"])
>                 .state == "SUCCESS", "should be success");
>         assert(cli.create_config(["create_config", 
> "--name=\"titi\""])
>                 .state == "SUCCESS", "should be success");
>         
> assert(collectException(cli.create_config(["create_config", 
> "--name="]))); //here is my problem
>     }
> ```
>
> i would like to specify `collectException!GetOptException`, but 
> it's seem's make the program exit with fail status.
>
> any idea what i'm doing wrong ?
>
> also it's my first d program, so if anything seem's bad let me 
> know

I haven't spot the exact position of the problem yet, but I think 
the usage of assert is not correct at same places of your coding.

- Assertions are for logic errors, not for issues related to 
resources (network, files, input from users,...)
You cannot recover from a logic error, there assert terminates 
the application.

- for resource issues you should throw Exceptions. You can 
recover from resource issues.

- assertions throwing "Errors" which are not catched by 
collectException.

Kind regards
Andre




More information about the Digitalmars-d-learn mailing list