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

MattTreichel picture MattTreichel · Jan 6, 2017 · Viewed 10.7k times · Source

I have this line come up in the console, only in Firefox, from my JavaScript application I'm developing:

Console log of Exception

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?

Answer

user6560716 picture user6560716 · Jan 19, 2017

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!