On Thursday, 11 June 2015 at 00:06:24 UTC, Yuxuan Shui wrote:
I want to know exactly what is considered to be 'throw'.
I'm able to use dynamic arrays (which can throw 'Range
violation') and asserts in a nothrow function. Shouldn't those
be considered 'throw'?
In D there are two types of exceptions: logic errors (derived
from 'Error') and environment exceptions (derived from
'Exception').
Logic errors are considered to be irrecoverable (eg. your program
should be terminated because people generally don't write code to
handle them - no one wraps their array accesses in try {...}
catch {...})
Environment exceptions are stuff like user input and network and
file access. This are problems that you generally want to handle
and that's why they're considered recoverable.
So 'Exception's propagate through function calls and 'Error's
terminate the program immediately.
A function 'f' marked as "nothrow" indicates that no exceptions
shall escape from it (if a function that 'f' calls can throw, 'f'
must written, so that it catches the exception), but since
'Error's terminate the program immediately the callers of 'f'
should not care about them and that's why 'Error's don't brake
the 'nothrow' promise of 'f'.