What is the point of nothrow?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jun 11 04:35:17 UTC 2018


On Monday, June 11, 2018 04:11:38 Bauss via Digitalmars-d-learn wrote:
> I'm very well aware that Error is not supposed to be caught and
> that the program is in an invalid state, but ehat I'm trying to
> get at is that if nothrow or at least a feature similar existed
> that could detect code that may throw Error, then you could
> prevent writing code that throws it in the first place.
>
> It would be a great tool to writing bugless code.

Well, the problem with that is that if you do anything involving assertions,
dynamic arrays, or GC memory allocations, you can get an Error. The same
goes for some stuff like final switch statements. Some of those Errors don't
happen with -release (e.g. the check on final switch and asserion failures),
but enough operations have to be checked at runtime that I expect that even
if you ignored the ones that don't happen with -release, relatively little
code could be guaranteed to not throw Errors. And most Errors are thrown
because of Error conditions that can't reasonably be caught at compile time.
So, while knowing that none of those Errors can happen in a particular piece
of code might be nice, all it really means is that you're not doing any
operations in that code which can be checked for error conditions at runtime
but which can't be checked at compile time. On the whole, what Errors are
really doing is catching the bugs that you didn't manage to catch yourself
and that the compiler can't catch for you, but the runtime can. So,
arguably, all you'd really be doing if you guaranteed that a piece of code
didn't throw any Errors is guarantee that the code didn't do any operations
where the runtime can catch bugs for you. As such, while you obviously don't
want to end up having any Errors thrown in your program, I seriously
question that trying to write code that is statically guaranteed to not
throw any Errors is very useful or desirable - especially since it's not
like such code is guaranteed to be bug-free. It just wouldn't have any of
the bugs that the runtime can catch for you.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list