Comparing Exceptions and Errors in D
schveiguy.com
schveiguy.com
[1] https://en.wikipedia.org/wiki/Substructural_type_system#Diff...
myFunc();
but not for auto foo = myFunc();
if myFunc returns a mustUse type.Question: Do you know if this would also be fine if the called function was in a return statement?
- if the expression is the top level expression in a statement (like in my example code) or the left hand side in a comma expression - if the expression is not an assignment, increment, or decrement
So using it in a return is fine because then the expression is no longer the top level in the statement, the return is. Of course then the function calling THAT function must deal with not discarding the value. It's pretty neat.
https://en.cppreference.com/w/cpp/language/attributes/nodisc...
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attribute...
Working on updating that.
I think the reason the documentation is cryptic is because it's a library-supplied User Data Attribute that is specially recognized by the compiler. The documentation generator is having trouble with it. In code it's pretty simple:
https://github.com/dlang/druntime/blob/705fb36e5fc4d930eed85...
I can fix it but the example given is clear, no?
e.g. if you return something that could contain an error then ignoring the return value could be disastrous.
> How it works is that the compiler still omits exception cleanup code, and the code that catches the Error is not allowed to continue the program. If it does, the program may obviously be in an invalid state.
Yikes? What's the rationale for allowing errors to be thrown out of nothrow functions, rather than just aborting there and then?
But the nice thing is, it reuses the same handling mechanisms as exceptions. You don't have to create something different for handling Errors. In fact, the code that prints error stack traces and exits is the same code that handles exceptions.
Consequently, this is why you shouldn't catch them, or at least not catch them and continue. The exceptions are unittests and contract asserts, where the compiler does guarantee proper stack unwinding.
In that case, why not just abort? Why give the user a footgun like this?
> Non-throwing functions are permitted to call potentially-throwing functions. Whenever an exception is thrown and the search for a handler encounters the outermost block of a non-throwing function, the function std::terminate or std::unexpected (until C++17) is called:
Microsoft's dialect of C++ has a nothrow attribute which is purely advisory, like D's, and is now deprecated in favour of noexcept [2]:
> We recommend that all new code use the noexcept operator rather than __declspec(nothrow).
> This attribute tells the compiler that the declared function and the functions it calls never throw an exception. However, it does not enforce the directive. In other words, it never causes std::terminate to be invoked, unlike noexcept, or in std:c++17 mode (Visual Studio 2017 version 15.5 and later), throw().
Rust has panics, which by default unwind, like exceptions, but can be compiled to abort instead [3]:
> By default, when a panic occurs, the program starts unwinding, which means Rust walks back up the stack and cleans up the data from each function it encounters. However, this walking back and cleanup is a lot of work. Rust, therefore, allows you to choose the alternative of immediately aborting, which ends the program without cleaning up.
I don't believe there is any stable or planned way to declare that normal Rust functions cannot panic (so should abort instead of letting the panic escape). But there is some work towards the idea that extern "C" functions should do so [4].
[1] https://en.cppreference.com/w/cpp/language/noexcept_spec
[2] https://docs.microsoft.com/en-us/cpp/cpp/nothrow-cpp?view=ms...
[3] https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-...
[4] https://github.com/rust-lang/project-ffi-unwind/blob/master/...