Null references and access violation

Bauss via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 14 09:52:19 PDT 2016


Can someone riddle me this, why D gives an access violation 
instead of ex. a null reference exception?

Like let's say you have a field that's a class and you forget to 
give it a value. Now that will cause an access violation, but 
generally access violations are an indication that you're program 
has reached a nasty state and can't be recovered. Hence why you 
cannot catch it by doing a normal catch block for "Exception", 
but has to do on "Throwable" (Idk if "Error" works for?) Now 
that's a problem, because in that particular case the program may 
still work without being in an unrecoverable state.

Of course it can be resolved by catching it, but what about 
actual unrecoverable access violations? You will be shadowing 
those too then.

This is especially a problem when you work with 3rd party 
libraries where you basically have no control over what happens.

Is there a way around this, to make thins like not assigning an 
instance to a class cause some null reference exception or is 
there a specific reason why it exactly throws an access violation 
error.


More information about the Digitalmars-d mailing list