newbie problem with nothrow

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 1 08:54:42 PDT 2016


On Tuesday, November 01, 2016 10:57:38 Steven Schveighoffer via Digitalmars-
d-learn wrote:
> On 10/31/16 6:29 PM, Jonathan M Davis via Digitalmars-d-learn wrote:
> > On Monday, October 31, 2016 22:20:59 Kapps via Digitalmars-d-learn 
wrote:
> >> Assuming you're sure it'll never throw. To enforce this, use try
> >> { } catch { throw new Error("blah"); }. You can still throw
> >> errors, just not exceptions (as errors are not meant to be
> >> caught).
> >
> > I always use assert(0). e.g.
> >
> > try
> >
> >     return format("%s", 42);
> >
> > catch(Exception)
> >
> >     assert(0, "format threw when it shouldn't be possible.");
>
> This turns into a non-printing seg fault when compiled in release mode.

I'm well aware of that, and I don't see that as a problem. A message might
be nice, but the key thing is that it kills the program if there's a
problem, and given Mike's point about the C layer, having it segfault is
potentially preferable to throwing an Error to kill the program.

> Is there not some assumeNoThrow wrapper somewhere?

Someone added assemWontThrow to std.exception semi-recently, but I'd be very
surprised if it didn't incur performance overhead such that I'd just as soon
use an explicit try-catch and be done with it.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list