john-wayne-interessierts

Hello,
after a look in some sourcecodes I am confused,
-what's the best way for a catch(error) output
-some gadgets use it for any function, is this realy needed
-If this is not needed which error should be catched, which not Or is try catch completly sensesless to catch errors

regards
Peter



Re: Sidebar Gadget Development try / catch what's the best way?

Jonathan Abbott

It's good practice to put try/catch around all any code that could generate an error. Possibly even go as far as putting try/catch around all code. This is because errors are suppressed and cause functions to terminate prematurely - which can cause strange Gadget behaviour.

If you use begin/endTransition - always place try/catch around the code between the statements, to ensure endTransition is called, or your Gadget may appear to hang:
System.Gadget.beginTransition();
try{
\\ update the Gadget
} catch (err) {}
System.Gadget.endTransition(System.Gadget.TransitionType.morph, 0.2);


Depending on your need, there's several ways to display errors:

1. Display the output to screen using Shell Popup, which is useful for quick debugging - but should not be used in a production Gadget:
var oShell = new ActiveXObject("WScript.Shell");
try{
// your code
} catch (err) {oShell.Popup(err.name + " - " + err.message)}

2. Write the output to the debug log, and use Sysinternals DebugView to watch the output:
try{
// your code
} catch (err) {System.Debug.outputString(err.name + " - " + err.message)}

3. Write the output to a file, which is ideal when you need users to provide debugging information:

var oFSO = new ActiveXObject("Scripting.FileSystemObject");
var bDebug = oFSO.FileExists(System.Gadget.path + "\\debug.txt");
try{
if (bDebug) var debugLogFile = oFSO.OpenTextFile(System.Gadget.path + "\\debug.txt", 2);
} catch(err) {bDebug = false; debugLog("Open debug.txt error" + err.name + " - " + err.message)}

function debugLog(str) {
try{
if (bDebug) debugLogFile.WriteLine(str);
else System.Debug.outputString(str);
} catch(err) {}
}

...

try{
// your code
} catch(err) {debugLog("<errorlocation>: " + err.name + " - " + err.message)}




Re: Sidebar Gadget Development try / catch what's the best way?

john-wayne-interessierts

Many thanks for this great answer.

regards
Peter





Re: Sidebar Gadget Development try / catch what's the best way?

Eike F

Thanks for this overview of how to handle the common debug tasks :)




Re: Sidebar Gadget Development try / catch what's the best way?

James Brundage

Another option is to go to Internet Properties - Advanced - and uncheck Disable Script Debugging (Other). You can do this on an as needed basis, another machine won't have it on by default, and it will tell you the line number (which err sadly does not).

James
Why limit yourself to one desktop of gadgets Try the Sidebar Switcher gadget.