Handling errors in SC3

The root error class in SC3 is Exception. The basic method is try which can be found in Function help file. For a tree hierarchy of errors see Error help file.

Handling syntax errors

The problem here is that try cannot catch syntax errors. A workaround to do implement this functionality in SC3 is to use the return value of a function. A Function with errors returns nil.

f = { "1+1+".interpret };

if(f.value == nil){
    "Syntax error in f"
}{
    f.value.postln;
}

Threads and error handling

When we would like to run a Routine (see fork) it is a good idea to use try to catch an error (see sc-users topic below).

fork { // Routine
    try {
        "1+1+".interpretERROR;
    }{ |error| [\errorInFork, error].postln; };
    1.wait;
    "There was an error?".postln;
}

Booting server

If we would like to check if a UGen is executable, with no syntax erros as described above, we can call s.waitForBoot, which is a kind of fork (see topic on sc-users).

The code block below uses protect to ensure that the server is running and a nested try in s.waitForBoot. This is because both fork and waitForBoot are callback functions.

try {
    fork {
        protect {
            s.waitForBoot {
                try {
                    { SinOsc.ar().notValidMethod }.play
                }{ |error| [\innerTry, error].postln; }
            };
            4.wait;
            s.quit;
        }{ |error| [\onProtect, error].postln; };
    }
}{ |error| [\outerTry, error].postln; }
SuperCollider 3 server ready.
Receiving notification messages from server localhost
Shared memory server interface initialized
[ innerTry, a DoesNotUnderstandError ]
server 'localhost' disconnected shared memory interface
/quit sent

[ onProtect, nil ]
JackDriver: max output latency 0.0 ms
jack main caught signal 12
RESULT = 0

Latest posts