/*
When you are developing an app I have a number of recommendations about error handlers like app.SetOnError()
1) add handlers as late in the development cycle as possible. Once you have a handler your assumptions about what is causing errors will usually be wrong
2) if you must turn on your handlers during development, make sure they give you as much information as possible. See the code below
3) if you get inexplcable behaviour from your app, suspect your error handler is faulty before checking anything else
4) if your handler is catching errors, try hard to find a way to avoid the errors before they happen rather than relying on the handler
5) if you change your error handler before release to hide information from the user, double and triple test the handler again. This applies even if the changes appear to be minimal
Having said all that, in complex apps, throwing a specific error and catching it can be a useful, sometimes elegant, way to return to a fixed point in your app and clean up
There are also times when there is a good reason to wrap short pieces of code in a try/catch/finally block when checking for a specific failure you know may occur but cannot easily check in advance
The poor practice tends to be catching errors you did not throw deliberately because it's easier than getting the code right.
And please never allow an error to occur in your app without reading the error message to confirm you know why it happened
*/
// simple SetOnError example
function OnStart()
{
app.SetOnError(OnError)
lay = app.CreateLayout("Linear", "VCenter,fillXY")
btn = app.AddButton(lay, "Test")
btn.SetOnTouch(btn_OnTouch)
app.AddLayout(lay)
}
function btn_OnTouch()
{
var test = Math.floor(Math.random() * 6)
if(test == 5) doStuff()
app.ShowPopup(test+"\nkeep trying")
}
function OnError(errmsg, line, file)
{
var msg =
'Message: "' + errmsg + '"\n' +
'Line: ' + line + '\n' +
'File: "' + app.Uri2Path(file) + '"';
yn = app.CreateYesNoDialog(msg)
yn.SetOnTouch(yn_OnTouch)
yn.SetButtonText("Exit", "Remain")
yn.Show()
}
function yn_OnTouch(ans)
{
if(ans == 'Yes') app.Exit()