📜  Node.js process.setUncaughtExceptionCaptureCallback() 方法

📅  最后修改于: 2022-05-13 01:56:36.718000             🧑  作者: Mango

Node.js process.setUncaughtExceptionCaptureCallback() 方法

process.setUncaughtExceptionCaptureCallback() 方法是处理模块的内置应用程序编程接口,用于设置发生未捕获异常时将调用的回调函数。回调函数将接收异常值作为其第一个参数。

句法:

process.setUncaughtExceptionCaptureCallback( callback_function )

参数:此方法接受一个如上所述和如下所述的参数。

  • callback_function:这是一个必需的参数。它可以是函数或空值。如果设置为 null,则该函数将取消设置回调函数。

返回:不返回任何值。

下面的例子说明了在 Node.js 中process.setUncaughtExceptionCaptureCallback() 方法的使用:

示例 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());


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");
}


输出:

false
true

示例 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");
}

输出:

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

注意:上述程序将使用node filename.js命令编译和运行,仅在 POSIX 平台上。

参考: https://nodejs.org/api/process.html#process_process_setuncaughtexceptioncapturecallback_fn