Node.js process.setUncaughtExceptionCaptureCallback() Method
The process.setUncaughtExceptionCaptureCallback() method is an inbuilt application programming interface of the processing module which is used to set a callback function which will be called when an Uncaught Exception occurs. The callback function will receive the exception value as its first argument.
Syntax:
process.setUncaughtExceptionCaptureCallback( callback_function )
Parameters: This method accepts a single parameter as mentioned above and described below.
- callback_function: This is a required parameter. It can be a function or null value. If it set to null, then the function will unset the callback function.
Return: It does not return any value.
Below examples illustrate the use of process.setUncaughtExceptionCaptureCallback() method in Node.js:
Example 1:
Javascript
// Allocating process module const process = require( 'process' ); // Function to be set as call back function to_be_called(ex){ console.log(ex); } // Checking whether any callback has been set before calling // process.setUncaughtExceptionCaptureCallback(to_be_called); console.log(process.hasUncaughtExceptionCaptureCallback()); // Setting callback process.setUncaughtExceptionCaptureCallback(to_be_called); // Checking whether any callback has been set before calling // process.setUncaughtExceptionCaptureCallback(to_be_called); console.log(process.hasUncaughtExceptionCaptureCallback()); |
Output:
false true
Example 2:
Javascript
// Allocating process module const process = require( 'process' ); // Function to be set as call back function to_be_called(ex) { console.log(ex); } // Checking whether any callback has been set before calling // process.setUncaughtExceptionCaptureCallback(to_be_called); // Printing whether a callback is set or not if (process.hasUncaughtExceptionCaptureCallback()) { console.log( "a callback has been set using " + "process.setUncaughtExceptionCaptureCallback() method" ); } else { console.log( "no callback has been set using " + "process.setUncaughtExceptionCaptureCallback() method" ); } // Setting callback if (process.setUncaughtExceptionCaptureCallback) { process.setUncaughtExceptionCaptureCallback(to_be_called); } else { console.log( "process.setUncaughtExceptionCaptureCallback() " + "method is not defined!" ); } // Checking whether any callback has been set before calling // process.setUncaughtExceptionCaptureCallback(to_be_called); if (process.hasUncaughtExceptionCaptureCallback()) { console.log( "a callback has been set using " + "process.setUncaughtExceptionCaptureCallback() method" ); } else { console.log( "no callback has been set using " + "process.setUncaughtExceptionCaptureCallback() method" ); } // Resetting callback if (process.setUncaughtExceptionCaptureCallback) { process.setUncaughtExceptionCaptureCallback( null ); } else { console.log( "process.setUncaughtExceptionCaptureCallback() " + " method is not defined!" ); } // Checking whether any callback has been set after resetting if (process.hasUncaughtExceptionCaptureCallback()) { console.log( "a callback has been set using " + "process.setUncaughtExceptionCaptureCallback() method" ); } else { console.log( "no callback has been set using " + "process.setUncaughtExceptionCaptureCallback() method" ); } |
Output:
no callback has been set using process.setUncaughtExceptionCaptureCallback() method a callback has been set using process.setUncaughtExceptionCaptureCallback() method no callback has been set using process.setUncaughtExceptionCaptureCallback() method
Note: The above program will compile and run by using the node filename.js command, only in POSIX platforms.
Reference: https://nodejs.org/api/process.html#process_process_setuncaughtexceptioncapturecallback_fn
Contact Us