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
相關用法
- Node.js GM border()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM whiteThreshold()用法及代碼示例
- Node.js GM modulate()用法及代碼示例
- Node.js GM edge()用法及代碼示例
- Node.js GM implode()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 Node.js | process.setUncaughtExceptionCaptureCallback() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。