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 moduleconst process = require('process');// Function to be set as call backfunction 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 callbackprocess.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 moduleconst process = require('process');// Function to be set as call backfunction 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 notif (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 callbackif (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 callbackif (process.setUncaughtExceptionCaptureCallback) { process.setUncaughtExceptionCaptureCallback(null);} else { console.log("process.setUncaughtExceptionCaptureCallback() " + " method is not defined!");}// Checking whether any callback has been set after resettingif (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





Please Login to comment...