why am I getting an undefined error?

I'm working on a javascript debugging tool, what I need at the moment is to get the line number off the end of a stack trace. So I wrote the following function to get a stack trace, remove the first few lines and then I was going to use indexOf(':') to get the line number. However I keep getting a "Cannot call method 'substring' of undefined" error. OK that should be an easy one to fix, but wait a minute - console.log suggest the file is defined. Can someone explain where I'm going wrong.

code:

var getLine = function () {
    var stack = (new Error('dummy').stack).toString();
    var stackLines = stack.split('\n');
    stackLines = stackLines.filter(function (element) {
        var exclude = [ "Error: dummy", "graph.js" ];
        for (var i = 0; i < exclude.length; i++) {
            if (element.indexOf(exclude[i]) !== -1) {
                return false;
            }
        }
        return true;
    });

    console.log("1 array", stackLines);
    console.log("2 element", stackLines[0]);
    console.log("3 typeof element", typeof (stackLines[0]));
    console.log("4 huh?", stackLines[0].substring(1));
}

output:

1 array [ '    at Object.<anonymous> (E:\\Development\\james\\main.js:52:18)',
  '    at Module._compile (module.js:456:26)' ]
2 element     at Object.<anonymous> (E:\Development\james\main.js:52:18)
3 typeof element string
E:\Development\james\graph.js:47
    console.log("huh?", stackLines[0].substring(1));
                                      ^
TypeError: Cannot call method 'substring' of undefined

the even stanger thing is - if i wrap the console.log statements in a try/catch then it executes without error?

code:

var getLine = function () {
    var stack = (new Error('dummy').stack).toString();
    var stackLines = stack.split('\n');
    stackLines = stackLines.filter(function (element) {
        var exclude = [ "Error: dummy", "graph.js" ];
        for (var i = 0; i < exclude.length; i++) {
            if (element.indexOf(exclude[i]) !== -1) {
                return false;
            }
        }
        return true;
    });
    try {
        console.log("array", stackLines);
        console.log("element", stackLines[0]);
        console.log("typeof element", typeof (stackLines[0]));
        console.log("huh?", stackLines[0].substring(stackLines[0].indexOf(":")));
    } catch (e){
        console.log("error",e);
    }

output:

1 array [ '    at Object.<anonymous> (E:\\Development\\james\\main.js:52:18)',
  '    at Module._compile (module.js:456:26)' ]
2 element     at Object.<anonymous> (E:\Development\james\main.js:52:18)
3 typeof element string
4 huh? :\Development\james\main.js:52:18)

I feel like I'm missing something very very obvious, but I'm not seeing it!

OK I got to the bottom of it, as I suspected there was a simple explanation:

The function was being called multiple times, the first time stackLines[0] was defined however one of the later times it was undefined. The confusing part is the error was being thrown before the console.logs were being printed.

So the first time it was called all the log statements would work, but none of them were actually output before it through an error in one of the subsequent calls.

When I ran it with the try catch I missed the error as there was a bunch of output and I was scrolling to the top and only checking the first one.

Thanks for the responses everyone :)