Tuesday, October 14, 2008

javascript exception catching and error printing

I hate seeing JavaScript errors on pages, especially on my pages, so in this case I have to run JavaScript debugger to catch the exact line there the problem is. If I change a function with many exceptions, a better solution exists: printing JavaScript errors with alert function (we firstly handle the error and then we print it):

try{
... do some javascript here ...
} catch (e) {
try{
var ex_joro = e; // this is to remember to set the exception name :)
var errorString =
ex_joro.name + ' ' + ex_joro.message
+ ', fileName = ' + ex_joro.fileName
+ ', lineNumber = ' + ex_joro.lineNumber
+ ", stack: " + ex_joro.stack
+ ",\n\n all: " + ex_joro
; // this is used when you do javascript debugging

alert( errorString );
} catch ( exception_666 ){
alert( "exception while trying to alert exception " + exception_666 );
}
}