16

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?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
MattTreichel
  • 1,418
  • 3
  • 18
  • 35
  • 1
    Use a different browser - Chrome will sometimes help you debug issues that Firefox (or Firebug) will not catch. – user2182349 Jan 06 '17 at 04:58
  • 2
    @user2182349 Well, this issue doesn't appear in Chrome at all. It's not really critical to solve, I'm just curious if there's any way to interact with a problem like this or why Firefox does it. – MattTreichel Jan 06 '17 at 17:41
  • 1
    when you open Debugger, on the debugger tab, when you click on the gear on the right hand side. there is an option called, pause on exception also want to deselect ignore caught exceptions, then reload page – tik27 Jan 18 '17 at 00:26
  • 1
    I once had a problem like that. It was because of Firefox limitation for the size of documents, which I believe it was 768k. Check the size of the document you're retrieving. – Luís Assunção Jan 18 '17 at 15:23
  • @tik27 Ah, that's the obvious sort of thing I'm didn't know about, thanks. Trying it though, it seems to ignore these "uncaught exception"s. So there's something irregular about them. If "Ignore Caught Exceptions" is checked it makes no difference either, but seems funny that if it's checked - the pausing explicitly ignores what Firefox itself calls an "uncaught" exception. – MattTreichel Jan 19 '17 at 17:10

3 Answers3

4

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 appreciate the effort, but I do know about console.log debugging. Running your '.split()' example, Firefox returns "TypeError: barStr is undefined" - a much more descriptive error than what I'm dealing with, so it makes me unsure if you know what I'm asking here. I did try the Firefox debugger command but it seemed to crash the browser when I'd interact with the page while paused, and while I didn't mention it my code is running continually on animation frames so console.logging and stepping through code isn't enough to catch the exact frame the error occurs. – MattTreichel Jan 19 '17 at 16:59
  • Ignore the `.split()` example code. Try putting `console.log("Dev console!")` in the top of your file and run it. If you see nothing in the console, you have a separate problem. Otherwise, put a `console.log()` after each line to find the offending line. –  Jan 20 '17 at 16:01
  • If this was the answer you are looking for or it answered you question well, please mark it as accepted. Otherwise, explain what you are looking for in a comment or an edit to your question. –  Jan 24 '17 at 01:56
  • 1
    I've already have edited it, clarifying "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." I'm not looking to identify the location of the bug which is what the majority of this answer focuses on, but why Firefox throws what seems to be a unique and unhelpful exception. The browser tools or try-catch mechanisms should help explain something like this if it was a normal exception, but it doesn't seem to be. – MattTreichel Jan 24 '17 at 02:33
  • 1
    Trial-and-Error console.logging my entire script seems like a really tedious exercise especially when combined with complex logic, and excessive console.logging seems to crash the browser if done every animation frame. – MattTreichel Jan 24 '17 at 02:44
  • Split it up into sections. First `console.log` once at the beginning of every animation frame. If you see only 1 log, you know an exception is being thrown the first time. If that happens, put a log at 0%, 25%, 50%, and 75% in the frame's code. If you see 2 items in the log, you know the exception occured between the 2nd and 3rd log statements (between 20% and 50%.) Then you can split that section of code into 0%, 25%, 50% and so on, until you get to 1 line. If it's a function call, split up the function, if not, that's the bug. [See graphical explanation](http://beta.programmer5000.com/d.png) –  Jan 24 '17 at 15:06
3

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');
Niebieski
  • 125
  • 6
  • Interesting, but I don't have any inline scripts, just one external file. I think it might have something to do with how Firefox handles exceptions in internal code. – MattTreichel Jan 24 '17 at 02:48
1

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

Trung Duong
  • 3,475
  • 2
  • 8
  • 9
  • I don't see this option in my about:config. I'm using Firefox 50 (the current version?). – MattTreichel Jan 24 '17 at 02:51
  • 1
    You could go to about:config, **right click** on list of preferences, then at the context menu, choose **New/Boolean**, enter *"dom.report_all_js_exceptions"* for name and *"true"* for value. – Trung Duong Jan 24 '17 at 03:11
  • 1
    Thanks, I tried it and restarted my browser but it doesn't seem to help with this problem. – MattTreichel Jan 24 '17 at 03:15
  • 1
    I've experienced this kind of error sometime. It's hard to find the cause. Sometime, this error could be raised from the code that use setTimeout function, you should check it. – Trung Duong Jan 24 '17 at 03:41
  • Hmm, I have quite a lot of code that uses setTimeout, so it'd be tough to find. But that's a nice possible clue. – MattTreichel Jan 24 '17 at 04:24
  • The common mistake which our staffs usually make is that they call function directly inside setTimeout, for example: they use *setTimeout(functionA(), 0)*, instead *setTimeout(function(){ functionA() }, 0)*. That cause the error undefined will be raised. – Trung Duong Jan 24 '17 at 05:34