Why does setInterval() ignore errors?

Eugene Yarmash

Javascript's setInterval() seems to pay no attention to whether or not the code it calls throws exceptions. For instance, this will not terminate the program but call the function over and over again:

setInterval(function() { throw "error" }, 1000);

What is the reason for this behaviour? Is it documented anywhere?

Dancrumb

The MDN docs for throw say, for a thrown object:

If no catch block exists among caller functions, the program will terminate.

This isn't strictly accurate. ECMAScript spec section 10.4

A thrown exception may also exit one or more execution contexts.

This is more accurate.

What is an execution context? Well, there's one for your original program. When you make a function call, a nested context is created while that function is running. A call to eval will make one. 10.4 covers this.

In general, when an exception is thrown, it will pass up through all of the nested execution contexts until it is caught, or it reaches the outer execution context. If it is not caught within an execution context, that context will be terminated. Thus, if your original program is the outermost execution context, it will terminate on an uncaught exception?

When your handler function is called, due to your setTimeout interval expiring, it is the outermost execution context. Thus, the uncaught exception terminates it, but it is not nested within your original program. So nothing else gets terminated.

The thread that's handling the timer expires is part of the JS engine, so you're not going to terminate that either. Thus, once per interval, a new execution context is created when your handler function is called and so it repeats.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this setInterval call not work?

From Dev

Why does Git ignore these folders?

From Dev

Why does FloatToStr ignore ThousandSeparator?

From Dev

Why does GLSL ignore return?

From Dev

Why does sudo ignore aliases?

From Dev

Why does bash ignore SIGTERM?

From Dev

Why Gulp does not ignore the file?

From Java

Ignore Typescript Errors "property does not exist on value of type"

From Dev

Why does AngularJS ignore $scope field change?

From Dev

Why does this code ignore the await and proceed anyway?

From Dev

Why does make ignore my modifications?

From Dev

Why does Python requests ignore the verify parameter?

From Dev

Why does Git ignore the .vs directory?

From Dev

Why does SQL Anywhere ignore the @ named parameter?

From Dev

Why does XhtmlTextWriter ignore custom attributes?

From Dev

Why does Linq ignore my where clause?

From Dev

Why does Yii ignore my .po file?

From Dev

Why does my Android transition ignore the TransitionListener?

From Dev

Why does expand.grid ignore options?

From Dev

Why does for /f not ignore blank lines?

From Dev

Why does HTMLDocument ignore white spaces

From Dev

Why does bitbake ignore CFLAGS in my makefile?

From Dev

Why does Python ignore LC_ALL?

From Dev

Why does my program ignore arguments after #?

From Dev

Why does Matlab ignore the first input always?

From Dev

Why does CMake ignore compiler directives

From Dev

Why does if expression ignore condition in F#?

From Dev

Why does AngularJS ignore $scope field change?

From Dev

Why does 'tree' command ignore directories but not files