Exceptions (try/catch/finally)
import java.lang.Exception; // JavaFX uses Java's Exception
class MyException extends Exception { // must extend Exception (more)
var msg : String
}
function testException() { // no exception declaration needed
throw MyException{ msg: "got a problem" };
assert(false); // never reached
}
var gotException = false;
try {
testException();
assert(false); // never reached!
}
catch (e : MyException) { // catches only MyException
gotException = true;
assert(e.msg == "got a problem");
}
catch (e : java.lang.RuntimeException) {
assert(false); // never reached
}
assert(gotException);
gotException = false;
try {
throw MyException{ msg: "got another problem" };
assert(false); // never reached!
}
catch (e) { // catch all Throwable
gotException = true;
assert((e as MyException).msg == "got another problem");
}
assert(gotException);
var finallyCounter = 0;
function testFinally(a: Integer) : Boolean {
try {
if (a < 0)
throw new java.lang.Exception("a was negative");
if (a == 0)
return false;
}
finally {
finallyCounter++; // always invoked after leaving try
}
return true;
}
testFinally(1);
testFinally(0);
try { testFinally(-1); } catch (e) { /* ignore exception */ }
assert(finallyCounter == 3);

