How to debug “uncaught exception: undefined (unknown)” in Firefox

不问归期 提交于 2019-12-03 23:50:00

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!

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');

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!