Error

From PlainOldWebserver

Jump to: navigation, search

I never really got into that whole error throwing phenomenon popular in Java. You could do the same in Javascript. I do like catching them, though. Here are some subtle line number issues. Only the third way is the correct way to find the line number in the SJS file. This shows that there is more under the covers of SJS than it appears.


  // OK
  <?sjs
    var a=-1;
    try {
      if(a < 0) {
        throw(new Error("A boundary error"));
      }
    } catch(e) {
      document.writeln("Error '"+e.message+"'");
    }
  ?>
  // WRONG, LINE NUMBER INACCURATE
  <?sjs
    try {
      document.writeln(b.not_defined);
    } catch(e) {
      document.writeln("Error '"+e.message+"' found on line "+e.lineNumber);
    }
  ?>
  // CORRECT
  <?sjs
    document.writeln(b.not_defined);
  ?>

Result:

  Error 'A boundary error' found on line 441
  Error 'b is not defined' found on line 439
  Error: /test.sjs:21 b is not defined
Personal tools
Navigation