ERights Home elang / kernel 
Back to: Escape Expression On to: Finally Expression

Catch Expression


Has approximately the conventional meaning -- evaluate the first expression (the attempt), but, if it throws a Throwable that matches the catch-clause's pattern, then evaluate the second expression (the handler). If the attempt exits in any other way, then the outcome of the catch expression is the outcome of the atttempt. If the attempt does throw a matching Throwable, then the outcome of the catch expression is the outcome of the handler.

BNF:
"try" "{"
    eExpr
"}" "catch" pattern "{"
    eExpr
"}"
XML DTD:
<!ELEMENT catchExpr (%eExpr;, %pattern;, %eExpr;)>
Java:
try {
    jExpr
} catch (Exception prob) {

/* Expand pattern with prob both as the specimen and as the problem to throw on match failure. */

    jExpr
}
Example:
try {
    doSomething()
} catch problem {
    println(problem)
} finally {
    stream.close()
}
in Kernel-E:
try {

    try {
        doSomething.run()
    } catch problem :any {
        println.run(problem)

    }
} finally {
    stream.close()
}
in XML:
<finallyExpr>
    <catchExpr>
        <callExpr>
            <Noun>doSomething</Noun>
            <Verb>run</Verb>
        </callExpr>
        <finalPattern>
            <Noun>problem</Noun>
            <Noun>any</Noun>
        </finalPattern>
        <callExpr verb="run>
            <Noun>println</Noun>
            <Verb>run</Verb>
            <Noun>problem</Noun>
        </callExpr>
    </catchExpr>
    <callExpr>
        <Noun>stream</Noun>
        <Verb>close</Verb>
    </callExpr>
</finallyExpr>
in Java:
try {

    try {
        E.call(doSomething, "run");
    } catch (Exception prob) {
        Object problem = prob;

        E.call(println, "run", problem)
    }
} finally {
    E.call(stream, "close");
}

One problematic case: if the pattern match of the catch-pattern against the Throwable itself throws a new Throwable rather than matching or not matching. In this case, we continue throwing (ie, rethrow) the original Throwable, under the assumption that the original Throwable is more important than a Throwable that happened in trying to catch it.

Unlike Java, in E one cannot use the catch expression to catch all non-local exits, even if they would match a catch-clause's pattern. Catch expressions cannot intercept an ejection. Likewise, if the match of the catch pattern performs a non-local escape, that exit proceeds instead of the original non-local exit.

The attempt is in one scope box and the pattern and handler together are in another.

 
Unless stated otherwise, all text on this page which is either unattributed or by Mark S. Miller is hereby placed in the public domain.
ERights Home elang / kernel 
Back to: Escape Expression On to: Finally Expression
Download    FAQ    API    Mail Archive    Donate

report bug (including invalid html)

Golden Key Campaign Blue Ribbon Campaign