www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std/process.d: nothrow functions which throw (in struct ProcessPipes)

reply Drone1h <drone1h gmail.com> writes:
Hello All,

In phobos/std/process.d, in the ProcessPipes struct, we can see a 
few functions which are marked with "nothrow", but which (under 
some conditions) throw:

      property File stdout()  safe nothrow
     {
         if ((_redirectFlags & Redirect.stdout) == 0)
             throw new Error("Child process' standard output 
stream hasn't "
                             ~"been redirected.");
         return _stdout;
     }

Would it be possible to explain this, please ? Or point me to 
some relevant documentation, please ?

Thank you.
Jul 21 2020
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 21 July 2020 at 12:44:23 UTC, Drone1h wrote:
 Would it be possible to explain this, please ?
nothrow only applies to Exception and its children. Error is a different branch. Error means you have a programming error and cannot be caught and recovered (though the compiler allows it anyway, it reserves the right to just abort the program instead of actually going through the try/catch system), so it doesn't count as a normal exception, even though it is still thrown. You are supposed to prevent Errors by fixing the bugs in your code, so in a final build they never happen.
Jul 21 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/21/20 8:49 AM, Adam D. Ruppe wrote:
 On Tuesday, 21 July 2020 at 12:44:23 UTC, Drone1h wrote:
 Would it be possible to explain this, please ?
nothrow only applies to Exception and its children. Error is a different branch. Error means you have a programming error and cannot be caught and recovered (though the compiler allows it anyway, it reserves the right to just abort the program instead of actually going through the try/catch system), so it doesn't count as a normal exception, even though it is still thrown. You are supposed to prevent Errors by fixing the bugs in your code, so in a final build they never happen.
We need a way to mark posts like "I got this one", so we don't spend the same time saying the same thing lol. -Steve
Jul 21 2020
parent Drone1h <drone1h gmail.com> writes:
Steven Schveighoffer, Adam D. Ruppe, I wish to thank you both for 
your explanations ! I have understood and I have created a small 
test program. Indeed, at least on Windows, the destructor of an 
automatic struct does not seem to run.

Have a wonderful day... and I hope to be bugging the community 
again soon !
Jul 23 2020
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/21/20 8:44 AM, Drone1h wrote:
 Hello All,
 
 In phobos/std/process.d, in the ProcessPipes struct, we can see a few 
 functions which are marked with "nothrow", but which (under some 
 conditions) throw:
 
       property File stdout()  safe nothrow
      {
          if ((_redirectFlags & Redirect.stdout) == 0)
              throw new Error("Child process' standard output stream 
 hasn't "
                              ~"been redirected.");
          return _stdout;
      }
 
 Would it be possible to explain this, please ? Or point me to some 
 relevant documentation, please ?
 
 Thank you.
nothrow only pertains to Exceptions, not Errors. Throwing an Error isn't guaranteed to unwind the stack properly (which is why it can be done within nothrow), and the program should exit if it ever happens. An Error is a programming mistake (it should never happen if you wrote your code correctly), whereas an Exception can happen due to environmental issues or user input. -Steve
Jul 21 2020