1

In php when we do some thing wrong then an error is shown with the line number where it has been committed. i want this same in javascript you will understand from my codes:

var varlist = {};
var private = {
 str : function(nameArg, valueArg, security){
     if (varlist.hasOwnProperty(nameArg) === true){
         throw  "Variable Existance : variable "+nameArg +" is already exist in your variable list and cannot be overwritten in private type";
     }else if(security === "h"){
         var stre = String(valueArg);
         var strnew = stre.replace(/\d/g, "");
     varlist[nameArg] = strnew;
     }else if(security === "l"){
         varlist[nameArg] = String(valueArg);
     }else if(security !== "h" || security !== "n"){
             varlist[nameArg] = String(valueArg);
         throw "Unexpected Security Level: Entered " +security+" is unexpected and the default security level is low(l)";
     }
     }
    }

now look at the last throw statement if the security is not "h" or "l" then it throws an exception as you can see. Now suppose i am in my code editor on line says 90 and there i write private.str("abc",9000,"o"); where "o" is not valid. so javascript should find that line where private.str("abc",9000,"o"); is written and then throw "Unexpected Security Level: Entered " +security+" is unexpected on line "+line_number+" and the default security level is low(l)"; and here for example line_number is 90. so javascript should throw this statement"Unexpected Security Level: Entered o is unexpected on line 90 and the default security level is low(l)";

I tried to search for this but none could help at my extent on knowledge

thanks!

anni
  • 290
  • 1
  • 3
  • 15

3 Answers3

1

you can use firebug or js console, or do it in code like this

var
line=(new Error).stack.split("\n")[4],
nr=line.slice(line.indexOf("at ")+2, line.length);

(How to get JavaScript caller function line number? How to get JavaScript caller source URL?)

Community
  • 1
  • 1
aelgoa
  • 1,193
  • 1
  • 8
  • 24
0

Javascript throws up errors by default too , but instead of printing it on the screen they are shown in the console. press F12 to open up the debug/developer mode of your browser and refresh your page. If any javascript error pops up , it will show you where it goes wrong.

using console.log you can also add your own "errors" to the console to track.

  • ok but the main thing is that i want to know where is the mistake has been committed while scripting – anni Mar 25 '14 at 16:11
0

You can get the stack trace anywhere in your code using this technique. This code throws and catches a fake exception and returns the content of the Error.prototype.stack. Browser support: Chrome, Firefox, IE (10 and above), Opera, Safari (6 and above).

kol
  • 27,881
  • 12
  • 83
  • 120