Pergunta

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!

Foi útil?

Solução

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?)

Outras dicas

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.

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).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top