Node detecting termination

node.js

Solution

var exceptionOccured = false;

process.on('uncaughtException', function(err) {
    console.log('Caught exception: ' + err);
    exceptionOccured = true;
    process.exit();
});

process.on('exit', function(code) {
    if(exceptionOccured) console.log('Exception occured');
    else console.log('Kill signal received');
});

Problem

Is it possible for a node script to detect if it was terminate from "external" (p.e.: kill) or internal by a script error? To be more precise: The first case should't not be a main process that "observe" its children, or a additional script that "looks" if the main script is still running. 2nd case: without putting everything in try-except-blocks. I m looking for a function that is called when an error would stop the script.

Original source