当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js process.setUncaughtExceptionCaptureCallback()用法及代码示例


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

用法:

process.setUncaughtExceptionCaptureCallback( callback_function )

参数:该方法接受上面提到和下面描述的单个参数。



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

返回:它不返回任何值。

以下示例说明了Node.js中process.setUncaughtExceptionCaptureCallback()方法的使用:

范例1:

// 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());

输出:

false
true

范例2:

// 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 callack 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 defied!"); 
} 
  
// 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 defied!"); 
} 
  
// 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

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

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




相关用法


注:本文由纯净天空筛选整理自gekcho大神的英文原创作品 Node.js | process.setUncaughtExceptionCaptureCallback() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。