“ uncaughtException”是process模塊中Process類的事件,當未捕獲的JavaScript異常一直冒泡回到事件循環時,將發出該事件。
用法:
Event:'uncaughtException'
參數:此事件不接受任何參數作為參數。
返回值:此事件隻返回回調函數以進行進一步的操作。
範例1:
index.js
// Node.js program to demonstrate the
// Process 'uncaughtException' Event
// Importing the modules
const process = require('process');
var fs = require('fs');
// Independent Block which will execute
setTimeout(() => {
console.log('\n')
console.log('Greetings from GeeksforGeeks');
}, 1000);
// Event 'uncaughtException'
process.on('uncaughtException', (error, source) => {
fs.writeSync(process.stderr.fd, error, source);
});
// Throwing an exception
nonexistentFunc();
console.log('This Block of code will not run');
使用以下命令運行index.js文件:
node index.js
輸出:
ReferenceError:nonexistentFunc is not defined Greetings from GeeksforGeeks
範例2:
index.js
// Node.js program to demonstrate the
// Process 'uncaughtException' Event
// Importing the modules
const process = require('process');
var fs = require('fs');
// Event 'uncaughtException'
process.on('uncaughtException', (error) => {
fs.writeSync(process.stderr.fd, error);
});
// Throwing our Error
throw new Error('Ran out of coffee')
使用以下命令運行index.js文件:
node index.js
輸出:
Error:Ran out of coffee
參考: https://nodejs.org/dist/latest-v16.x/docs/api/process.html#process_event_uncaughtexception
相關用法
- Node.js process.nextTick()用法及代碼示例
- jQuery UI dialog create(event, ui)用法及代碼示例
- jQuery UI dialog close(event, ui)用法及代碼示例
- jQuery UI dialog resizeStart(event, ui)用法及代碼示例
- jQuery UI dialog dragStop(event, ui)用法及代碼示例
- jQuery UI dialog dragStart(event, ui)用法及代碼示例
- jQuery UI dialog resize(event,ui)用法及代碼示例
- jQuery UI dialog open(event,ui)用法及代碼示例
- jQuery UI dialog focus(event,ui)用法及代碼示例
- Node.js Process exit用法及代碼示例
- Node.js Process message用法及代碼示例
- Node.js Process beforeExit用法及代碼示例
- Node.js Process warning用法及代碼示例
- Node.js Process multipleResolves用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js Process uncaughtException Event。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。