Is there a bug in Javascript?

I would ask about java script error, is there an error type like php or others,

example: In php we noticed and Parse Error..etc notification will not be stop php execute, but parse will stop php code execution directly ..

now there is a js error like this, or what is js classification error. I know we can handle the error, try it, catch it .. but is there a bug in the js the script was tilted and the others didn't stop executing the script

Thank you

+3


source to share


2 answers


is there a bug in js, the script is tilted and others will not stop the script execution

Except for syntax syntax errors, no.

There are exceptions in JavaScript. The exception comes out of the code in which it is thrown, and the code that caused it, and so on, until it catches. If it is not caught, all current functions are terminated and the error is logged to the web console.

So an exception (either one of them that you throw explicitly, or something that happens as a byproduct of what you do) will either stop executing all running functions (if not caught), or only terminate some code (if it caught).

For example:

function foo() {
    try {
        bar(0);
    }
    catch (e) {
        console.log("Caught exception");
    }
}
function bar(a) {
    if (a <= 0) {
        throw new Error("'a' cannot be <= 0");
    }
    console.log("bar: a = " + a);
}

foo();

      

There the code in bar

except does not run (we do not see "bar: a = 0"

) because an exception was thrown, completion bar

. But the foo

code continues, in block catch

, because it foo

caught the exception.

JavaScript is unusual in that you can throw anything, including a string, a number, etc. But if you want useful information, you usually throw Error

:

throw new Error("Optional message here");

      

Since what you throw can be anything, you might think that there is a way to only catch certain things, but this is not the case. catch

catches any exception that was thrown. So:

try {
    throw "foo";
}
catch (e) {
}

try {
    throw new Error();
}
catch (e)
}

try {
    throw 42;
}
catch (e)
}

      

Note that those sentences are catch

identical; they catch anything that has been thrown away. You can of course check what you have and re-cast:

try {
    // ...some code here that may throw any of several things...
}
catch (e)
    if (typeof e === "string") {
        // Handle it here
    }
    else {
        throw e;
    }
}

      

Here we handle exceptions that are strings, not those that are numbers, Error

objects, etc.

You can create your own derived versions Error

if you like, although this is a bit of a pain than it should be:



function MySpecificError(msg) {
  this.message = msg;
  try {
    throw new Error();
  }
  catch (e) {
    this.stack = e.stack;
  }
}
MySpecificError.prototype = Object.create(Error.prototype);
MySpecificError.prototype.constructor = MySpecificError;

      

Then:

throw new MySpecificError("Something went wrong.");

      

Note that we needed to populate the code in in MySpecificError

order to create the stack trace. (Also note that not all engines provide a stack trace, but if so, it allows you to use it.)

Some engines provide several types of errors out of the box:

  • Error

  • RangeError

    (something was out of range)
  • ReferenceError

    (but usually this is what you let the engine quit)
  • TypeError

    (again)
  • SyntaxError

    (again)

Finally, it's worth noting that a few things that can throw exceptions in environments other than JavaScript, mostly around math. For example:

var result = 10 / 0;

      

In many non-JavaScript environments, this results in a runtime error (division by zero). This is not the case in JavaScript; result

gets the value Infinity

.

Similarly:

var x = Number("I am not a number");

      

or

var x = parseInt("I am not a number", 10);

      

... does not raise a parse error, sets x

to NaN

("not a number").

+4


source


Yes. Javascript errors can be types, and there is a standard hierarchy of error types. You can also write your own code to "throw" things that are not error objects.

(Actually, since the sentence catch

in Javascript / ECMAScript does not differ depending on the type of exception, the exception handling tends to be quite crude, i.e. "catch all errors" and then try to recover Hence, in the first order it doesn't matter what you throw

.)

The ECMAScript 5.1 specification states that syntax errors are "early errors" and that they must be submitted before the program can run. The exception is a syntax error encountered when running code using eval

. However, the spec does not say how to report early bugs or what happens afterwards. (At least I don't think so ...)



I believe it is a general strategy for a Javascript parser / compiler / interpreter to go to the closing block and replace the affected code with code that throws an exception (for example SyntaxError

) if it is run.

Literature:

+1


source







All Articles