node.jserror-handlingtry-catchfreezefunction-calls

Node JS not terminating after a chain of function calls


My code structure is something like this:

    A();

function A() {
    try {
        // something that can generate an error
        console.log('\nIn A()');
        B();
    } catch (e) {
        console.log(e);
    }
}

function B() {
    try {
        console.log('\nIn B()');
        C()
    } catch (e) {
        console.log(e);
    }
}

function C() {
    try {
        console.log('\nIn C()');
    } catch (e) {
        console.log(e)
    }
}

What I have observed is, if an exception gets generated, it comes into the catch block, prints the console.log. But does not terminate the code. I have read that catch won't terminate, but since the control is not transferred elsewhere, why does the process hang? I also tried making a function that logs the error and is called from catch block. It goes into the function & hangs again.

I am new to error handling in Node JS. Any help would be appreciated.


Solution

  • It had nothing to do with the cascading function calls. It was because the read stream used for taking user input from the commandline was not closed. Node does not terminate if a stream is left open or any asynchronous call is yet to be completed.