The C version:
int* foo(int bar) {
int* return_value = NULL;
if (!do_something(bar))
goto error_1;
if (!init_stuff(bar))
goto error_2;
if (!prepare_stuff(bar))
goto error_3;
return_value = do_the_thing(bar);
error_3: cleanup_3();
error_2: cleanup_2();
error_1: cleanup_1();
return return_value;
}
The D version: int* foo(int bar) {
scope(exit) cleanup1();
if (!do_something(bar))
return null;
scope(exit) cleanup2();
if (!init_stuff(bar))
return null;
scope(exit) cleanup3();
return do_the_thing(bar);
}
https://dlang.org/articles/exception-safe.html