Question

I currently have a try/finally block of this format:

try {
  var someOtherObject = new SomeOtherObject(param1, param2);
  someOtherObject.doStuff();

  // Object that basically holds a 'result set' of csv rows.
  var csv = new CsvObject();
  csv.openCSV(); // does not throw an error
  do {
    try {
      // Code that grabs stuff from the csv in a straightforward fashion.
    } catch (e) {
      log.info(e); // Log the error and continue on.
    }
  } while (csv.hasNext());      

} finally {
  csv.closeFileCSV(); //throws a TypeError: Cannot call closeFileCSV() on undefined.
}

No errors are thrown in the loop, nor anywhere else that I can tell outside the finally block. Can anyone give me clues as to why it would throw a TypeError when calling closeFileCSV in the finally block? I'm not a javascript expert but it doesn't seem like scope should be a problem here. I know that csv gets initialized correctly, because the try block uses the object to do stuff and no errors are thrown.

I'm hoping I'm just not seeing something obvious. Let me know if more code needs to be pasted in order to solve this.

Thanks.

Was it helpful?

Solution

I've determined that the variable, someOtherObject, is returning a null object. So when the doStuff() method is called, an exception is thrown. However, the finally block is immediately fired once this exception happens; and inside the finally block, the TypeError is thrown because csv has not been initialized in the try block yet.

This latter error is the only one that gets shown however, which was the source of confusion. Once I resolved the issue with someOtherObject, the code worked. (I've also added guards around the close method to make sure that csv is initialized).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top