No thanks.
Posted Nov 11, 2012 9:54 UTC (Sun) by
paulj (subscriber, #341)
In reply to:
No thanks. by Cyberax
Parent article:
Haley: We're doing an ARM64 OpenJDK port!
You can structure your code (in C, C++, Java, etc) so that you only have to check for errors at object creation, and thereafter only where it is convenient - i.e. you can do as many operations on the objects as you want without having to check for erros, without any use of control-flow branch points exploding exceptions:
do {
var f = foo_new ();
var b = bar_new ();
if (!f || !b)
break;
f.twiddle ();
b.jiggle ();
<do as many more operations as you want on f and b,
without need for checking for errors>
if (f.state == ERROR || b.state == ERROR)
break;
<do whatever updates to this objects internal state,
and update its .state appropriately>
return;
} while (0);
if (f)
foo_delete (f);
if (b)
bar_delete (b);
<set this object to its error state>
return;
You do this by having the objects keep a finite state machine internally, with an idempotent error state. You could do more sophisticated error recovery by also tracking the prior state, that transited into the error state and potentially other more detailed info, e.g. with:
if (b.state == ERROR) {
switch (b.prev_state) {
...
}
}
Most of the same benefit of exceptions - of being able to move error handling out of the primary flow of code - but without the control-flow explosion (no need for goto either).
(
Log in to post comments)