3 ms·
Not sure why I am getting downvotes for this. The parent proposed a language feature to support an enforced code execution when a return value was not available
by r_c_a_d 8y ago
Not sure why I am getting downvotes for this. The parent proposed a language feature to support an enforced code execution when a return value was not available:
foo_t *maybe_get_foo(/*...*/) {
if (/*foo_is_available*/) {
return foo;
} else {
return NULL;
}
}
foo_t *f = maybe_get_foo();
if (!f) { /*...*/ } // REQUIRED or compile error
do_something(f->bar); // only allowed after NULL check
And I was pointing out that C++ exceptions do exactly that.
foo_t *maybe_get_foo(/*...*/) {
if (/*foo_is_available*/) {
return foo;
} else {
throw FooNotAvailable();
}
}
try {
foo_t *f = maybe_get_foo();
do_something(f->bar); // f cannot be NULL
}
catch (const FooNotAvailable& ex) {
/*...*/
}
Sorry if that wasn't the answer you wanted because C++ is not fashionable. But it is a valid option for how to avoid the use of NULL.