Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there type of error in Javascript?

Tags:

javascript

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

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

now is there js error like this, or what is the js classification error .. I know we can handle error by try, catch ..,but is there error in js was stooped script and others will not stop execute script

thank you

like image 430
Anees Hikmat Abu Hmiad Avatar asked Mar 18 '23 14:03

Anees Hikmat Abu Hmiad


2 Answers

is there error in js was stooped script and others will not stop execute script

Not except for parsing/syntax errors, no.

JavaScript has exceptions. An exception exits the code in which it is thrown, and the code that called that, and so on until it's caught. If it isn't caught, then all currently-running functions are terminated and the error is logged to the web console.

So an exception (either one you throw explicitly or one that happens as a by-product of something you do) will either terminate all running functions (if not caught) or only terminate some code (if 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 following the exception is not run (we don't see "bar: a = 0") because an exception was throw, terminating bar. But foo's code continues, in the catch block, because 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 be thinking there's a way to catch only certain things, but that's 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 catch clauses are identical; they catch anything that was thrown. Of course, you can then inspect what you got and re-throw:

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

There we only handle exceptions that are strings, and not ones that are numbers, Error objects, etc.

You can create your own derived versions of Error if you like, although it's a bit more of a pain than it ought to 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 had to fill in the code in MySpecificError to create the stack trace. (Also note that not all engines provide a stack trace, but if they do, this lets you use it.)

Some engines provide a few error types out of the box:

  • Error
  • RangeError (something was out of range)
  • ReferenceError (but usually that's something you'd let the engine throw)
  • TypeError (again)
  • SyntaxError (again)

Finally, it's worth noting that several things that would cause exceptions in other environments don't in JavaScript, mostly around math. For instance:

var result = 10 / 0;

In many non-JavaScript environments, that results in a runtime error (division by zero). In JavaScript, it doesn't; result gets the value Infinity.

Similarly:

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

or

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

...doesn't throw a parsing error, it sets x to NaN ("not a number").

like image 168
T.J. Crowder Avatar answered Mar 28 '23 16:03

T.J. Crowder


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

(In fact, since the catch clause in Javascript / ECMAScript does not discriminate based on the type of the exception, exception handling tends to be rather crude; i.e. "catch all errors" and then attempt to recover. Hence, to a first order, it doesn't matter what you throw.)

The ECMAScript 5.1 spec says that syntax errors are "early errors" and that they must be reported before the program is executed. An exception to this is syntax errors detected in code being run using eval. However, the spec doesn't say how early errors are to reported, or what happens afterwards. (At least, I don't think it does ...)

I believe that a common strategy for a Javascript parser/compiler/interpreter to skip to the enclosing block, and replace the affected code with code that throws an exception (e.g. SyntaxError) if it is run.

References:

  • http://www-archive.mozilla.org/js/language/js20-1999-02-18/error-recovery.html
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError
  • EcmaScript 5.1 - Errors
like image 35
Stephen C Avatar answered Mar 28 '23 17:03

Stephen C