问题
I have this line come up in the console, only in Firefox, from my JavaScript application I'm developing:
It seems relatively harmless, but I'm curious if there's any way to deduce its origin, because it must come from somewhere, even if it claims 'unknown'. Wrapping the entire script in a try/catch block and toggling Firefox's "Pause on Exception" setting doesn't do anything, which seems to imply it's a special exception? I have some ideas what parts of my code might be causing it that are using Working Draft APIs, but I'm more interested in why it reports this way and what I can do about it. Does Firefox not provide any more detail?
回答1:
There's a few ways you could try to squash this bug.
One thing that's very tedious but will get you the line number of the exception is code that looks like:
foo();
console.log("Line 1");
bar();
console.log("Line 2");
baz();
console.log("Line 3");
and so on, and if you get this in the console:
Line 1
Line 2
Uncaught exception: undefined
then you know that baz() was causing the error. Another way is to use the debugger, like so:
debugger;
foo();
bar();
baz();
and you can use firefox's debugger to go over each line and see which one throws the error to the console.
If you have a lot of code, you could try the divide-and-conquer trick, like this:
var fooStr = foo();
var fooArr = fooStr.split("");
fooArr = fooArr.reverse();
foo(fooArr.join(""));
console.log("Block one");
var barStr = bar();
var barArr = barStr.split("");
barArr = barArr.reverse();
bar(barArr.join(""));
console.log("Block two");
var bazStr = baz();
var bazArr = bazStr.split("");
bazArr = bazArr.reverse();
baz(bazArr.join(""));
console.log("Block three");
Then, if the console looks like this:
Block one
Uncaught exception: undefined
Then the problem is in block 2. Then, you could do this:
var barStr = bar();
console.log("Line 1");
var barArr = barStr.split("");
console.log("Line 2");
barArr = barArr.reverse();
console.log("Line 3");
bar(barArr.join(""));
console.log("Line 4");
console.log("Block two");
console.log("Line 5");
And if you see:
Line 1
Uncaught exception: undefined
Then you know that var barArr = barStr.split("");
is your problem. From then, you might want to log variable values, like this:
console.log(barStr);
var barArr = barStr.split("");
And if you see this in the console:
undefined
Uncaught exception: undefined
Then you know that bar()
is returning undefined
(instead of a string), which does not have a split
method. Then you look at the code of bar to determine if, say you forgot a parameter? Mabey bar
looks like this:
function bar(value){
return strings[value];
}
and strings
is an object with something in it. Therefore, strings[undefined]
will return undefined
, which does not have a split
method. Bug squashed!
回答2:
I have found a simple example that reproduces the error you see.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
throw undefined
</script>
</head>
<body>
</body>
</html>
The firefox console shows:
uncaught exception: undefined (unknown)
Which is exactly what you get. This leads me to believe that the error occurs in a script that was embedded in your html.
Knowing that you may want to move all such scripts to their own files so that you can debug your code normally.
UPDATE
Accidentally I have found another way you can get such an error. And it comes in the form of eval
.
index.html
<!doctype html>
<html>
<head>
<script src="script.js"></script>
</head>
<body></body>
</html>
script.js
eval('throw undefined');
回答3:
You could go to about:config and create a boolean preference dom.report_all_js_exceptions.
This will force a lot more exceptions will show up in Error Console.
https://developer.mozilla.org/en-US/docs/Archive/Mozilla/Exception_logging_in_JavaScript
来源:https://stackoverflow.com/questions/41499109/how-to-debug-uncaught-exception-undefined-unknown-in-firefox