Why do associative arrays throw an Error (RangeError) on value not found?

Jacob Carlborg doob at me.com
Tue Dec 31 04:55:58 PST 2013


On 2013-12-31 13:42, Supernova wrote:
> Why do associative arrays throw an Error (RangeError) on value not found?
>
> This seems like it would be inefficient to check for, so a recoverable
> Exception (ItemNotFoundException?) would seem to be more appropriate.

How would ItemNotFoundException be any more efficient? The idea is that 
the error should not be recoverable. If you get an RangeError in your 
code you have made a logical error.

You need to explicitly check if a key is available in the associative 
array before accessing it, something like this:

if (auto value = key in aa)
     writefln("key %s was found with the value %s", key, value);

else
     writefln("key %s was not found", key);

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list