// callback is a Method object
function foo(callback) {
try {
callback.invoke();
} catch (ex) {
// handle potential exceptions raised by invoked method
}
}
However, when doing this I get an [FONT=Lucida Console]Unhandled Exception[/FONT] error with a strange stack trace that includes entries for code that is obviously not part of the call stack that raised the exception.
I have created a test suite to demonstrate the behavior:
using Toybox.Application as App;
using Toybox.Lang;
class BugExceptionsApp extends App.AppBase {}
function doesNotRaiseException() {
// No-op
}
function raisesException() {
throw new Lang.Exception();
}
(:test)
function testNoException(logger) {
try {
doesNotRaiseException();
} catch (ex) {
return false;
}
return true;
}
(:test)
function testNoExceptionInInvocation(logger) {
var mthd = new Lang.Method($, :doesNotRaiseException);
try {
mthd.invoke();
} catch (ex) {
return false;
}
return true;
}
(:test)
function testException(logger) {
try {
raisesException();
} catch (ex) {
return true;
}
return false;
}
(:test)
function testExceptionInInvocation(logger) {
var mthd = new Lang.Method($, :raisesException);
try {
mthd.invoke();
} catch (ex) {
return true;
}
return false;
}
The last test fails with this output:
Executing test testExceptionInInvocation...
null
raisesException in C:\Users\Josh.Wilson\eclipse\workspace\BugExceptions\source\BugExceptionsApp.mc:11
doesNotRaiseException in C:\Users\Josh.Wilson\eclipse\workspace\BugExceptions\source\BugExceptionsApp.mc:6 // <--- Not part of the actual call stack
testExceptionInInvocation in C:\Users\Josh.Wilson\eclipse\workspace\BugExceptions\source\BugExceptionsApp.mc:50
runTest in UnitTests:56
Unhandled Exception
Failed invoking <symbol>
ERROR
Bug info:
Environment:
OS: Windows 10
Eclipse Version: Neon.2 Release (4.6.2), Build id: 20161208-0600