Do any javascript runtimes (browsers, Node, etc.) ever throw uncatchable exceptions? Are any and all exceptions ever encountered in a javascript environment catchable in a try/catch statement?
-
1Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask– gnatCommented Dec 8, 2014 at 11:56
-
Exceptions from asynchronous callbacks are hard (but not impossible) to catch. Basically it depends on the environment that provides the async functionality.– BergiCommented Dec 8, 2014 at 16:40
-
Does this question belong to programmers.se ?– dynamicCommented Dec 8, 2014 at 17:00
-
There are silent security errors that are not catchable, because catching them would reveal information about the system hardware. It would be difficult to argue if these were actually Javascript errors or not.– ReactgularCommented Dec 8, 2014 at 22:30
3 Answers
If by exceptions you mean any exceptional condition that breaks your script, then all of them can throw uncatchable exceptions, since most syntax errors aren't catchable. Only syntax errors from dynamically evaluated code (eval
, new Function
) can be caught.
try { :( } catch(e) { } // uncatchable syntax error
That's assuming you mean catchable using try..catch
. Technically you could use the error
event to trap syntax errors from other script blocks:
<script> onerror = function (e) { return true; }; </script>
<script> :( </script>
On the other hand, maybe you don't consider errors that happen before evaluation to be exceptions. In that case "uncatchable exceptions" may be relegated to exceptions throws from other execution contexts (such as a function invoked with setTimeout), where you don't have control over the execution context throwing the exception. Of course, these exceptions will not disrupt the flow of your program.
-
11Good answer. I'll just point out that the category of uncatchable errors are called "early errors" in the ECMAScript 5 spec. Early errors are a superset of syntax errors (as they also include some illegal declarations not related to syntax). Commented Dec 8, 2014 at 14:40
-
@apsillers good point. IIRC everything listed there boils down to SyntaxError anyway in Chrome and Moz, except the PutValue thing is a ReferenceError. Commented Dec 8, 2014 at 15:41
-
1
-
1@JuanMendes good question. The spec never defines "exception," but it seems pretty consistent in using language like "throws a SyntaxError exception" when dealing with things that happen during evaluation, and language like "is a SyntaxError" when dealing with things that happen before evaluation. It does make sense to think of it as not being an exception if it happens before evaluation, but I'm not sure that's what the OP had in mind. Commented Dec 8, 2014 at 17:21
-
@Hey: You'll want to read the last paragraph of the algorithm conventions :-)– BergiCommented Dec 8, 2014 at 18:12
Some errors are really uncatchable (at least at the time of writing this post). Try to put this in Google Chrome's console:
try {
var elm = document.createElementNS("http://www.w3.org/2000/svg", "text");
elm.setAttribute("transform", "translate(106.7 NaN)");
} catch (e) {
console.log('caught:', e);
}
We expect to see "caught:"
and then an error data, but instead you'll see this right away:
`Error: <text> attribute transform: Expected ')', "translate(106.7 NaN)".`
-
I have to admit: it's caused by SVG subsystem of the browser, which is not that friendly with catching errors. But, to be fair, neither is CSS subsystem. And there are good reasons for that behavior. But anyway, developer should be aware of those weirdnesses Commented Sep 20, 2022 at 19:56
To generalize the other answer - exceptions that are asynchronous are generally are impossible to handle without the "bug guns" designed especially to handle them - that is domains and the process "uncaughtException"
event in node and onerror
in the browser.
The simplest way to get such an error would be:
setTimeout(function(){
throw "Catch me if you can!";
});
This is what you're seeing in the http.get({host:host, port:80}, console.error);
in the example of the other answer.
-
I think there's something more to the exception from http.get. I can catch an exception thrown from setTimeout with onerror, but not the one http.get throws (although it is thrown in an async way, processing continues after the call to http.get). Commented Dec 8, 2014 at 22:56
-
@Hey that's because
onerror
is a browser tool, the node equivalent would be to useprocess.on("uncaughtException", function(e){ ...
Commented Dec 8, 2014 at 23:07 -
Oops, I meant uncaughtException for node. I just realized node isn't catching anything with uncaughtException, though. Maybe that feature get scrapped? I guess the errors thrown in a separate execution context are catchable or not depending on whether you're willing to modify someone else's code (or use crap like onerror/uncaughtException). Commented Dec 8, 2014 at 23:26
-
Show me your code - uncaughtException works in node just fine. Commented Dec 8, 2014 at 23:27
-
process.on("uncaughtException", function(e){ console.log('hi') }); throw 'woo'
Commented Dec 8, 2014 at 23:28