Ken (Chanoch) Bloom's Blog

2nd March 2009

Error handling/cleanup patterns

I was reading some articles about the use of goto for error cleanup in C code, and started thinking about how one could mimic some of the elegance of goto for error cleanup in the presence of exceptions. Obviously goto itself is out of the question because you don't have a place to do an explicit goto when you encounter an error, and in Java you don't even have a goto statement.

Robert Love (on LKML) shows a very elegant example of cleanup code in C, using goto:

do A
if (error)
    goto out_a;
do B
if (error)
    goto out_b;
do C
if (error)
    goto out_c;
goto out;

out_c:
undo C
out_b:
undo B:
out_a:
undo A

out:
return ret;

In this code, if you have an error after doing a few steps of initialization, you jump to the appropriate place in the reverse-order undo code, and continue from there. The most obvious similar construct in C++ or Java with exception handlers would be to have several levels of nesting:

try{
    do A
    try{
        do B
        try{
            do C
        }catch(Exception){
            undo C
            throw;
        }
    }catch(Exception){
        undo B
        throw;
    }
}catch(Exception){
    undo A
    throw;
}

But a more elegant way to do cleanup would be to take advantage of the fall-through in a switch statement:

int progress=0;
try{
    do A
    progress++;
    do B
    progress++;
    do C
    progress++;
}catch(Exception){
    switch(progress){
    case 3:
        undo C
        //break intentionally omitted
    case 2:
        undo B
    case 1:
        undo A
    case 0:
    }
}

Isn't that more readable?

Permalink | c, c++, java.
My Website Archives

Tags