|
| 1 | + |
| 2 | +//************ javascript 错误 *************** |
| 3 | +// `<EvalError>` eval 执行 出错, 该错误类型已经不再在ES5中出现了,只是为了保证与以前代码兼容,才继续保留 |
| 4 | +// eval('xxx'); //ReferenceError: xxx is not defined |
| 5 | + |
| 6 | +// `<SyntaxError>` js 语法错误 |
| 7 | +// const i-1; // SyntaxError: Missing initializer in const declaration |
| 8 | + |
| 9 | +// `<RangeError>` 数组越界错误 |
| 10 | +// const array = new Array(-1); // RangeError: Invalid array length |
| 11 | + |
| 12 | +// `<ReferenceError>` 引用错误 |
| 13 | +// const a = b; // ReferenceError: b is not defined |
| 14 | + |
| 15 | +// `<TypeError>` 对象类型错误 |
| 16 | +// const c = (null).c; // TypeError: Cannot read property 'c' of null |
| 17 | + |
| 18 | +// `<URIError>` url 错误 |
| 19 | +// decodeURI('%'); // URIError: URI malformed |
| 20 | + |
| 21 | + |
| 22 | +//****************** 操作系统错误 ************************** |
| 23 | +// 由底层操作系触发的系统错误,例如试图打开不存在的文件 |
| 24 | +// const fs = require('fs'); |
| 25 | +// fs.openSync('x.js'); // Error: ENOENT: no such file or directory, open 'x.js' |
| 26 | + |
| 27 | +// 权限不足 |
| 28 | +// const os = require('os'); |
| 29 | +// os.setPriority(-1); //SystemError [ERR_SYSTEM_ERROR]: A system error occurred: uv_os_setpriority returned EACCES (permission denied) |
| 30 | + |
| 31 | +// 端口占用 |
| 32 | +// const http = require('http'); |
| 33 | +// http.createServer().listen(3000); |
| 34 | +// http.createServer().listen(3000); // Error: listen EADDRINUSE: address already in use :::3000 |
| 35 | + |
| 36 | + |
| 37 | +// ******************* 用户自定义错误 **************** |
| 38 | +// throw new Error('CustomError'); // Error: CustomError |
| 39 | + |
| 40 | + |
| 41 | +// ******************* 断言错误 **************** |
| 42 | +// const assert = require('assert'); |
| 43 | +// assert.strictEqual(1, 2, 'assert error'); // AssertionError [ERR_ASSERTION]: assert error |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +// 错误捕获 |
| 49 | + |
| 50 | +// error first |
| 51 | +// const fs = require('fs'); |
| 52 | +// fs.readFile('xxx.js', (err, data) => { |
| 53 | +// if (err) { |
| 54 | +// console.log(err); // { [Error: ENOENT: no such file or directory, open 'xxx.js'] errno: -2, code: 'ENOENT', syscall: 'open', path: 'xxx.js' } |
| 55 | +// return; |
| 56 | +// } |
| 57 | +// console.log(data); |
| 58 | +// }); |
| 59 | + |
| 60 | +// try catch |
| 61 | +// try { |
| 62 | +// a ++; |
| 63 | +// } catch (e) { |
| 64 | +// console.log(e); //ReferenceError: a is not defined |
| 65 | +// } |
| 66 | + |
| 67 | +// // EventEmitter on error |
| 68 | +// const EventEmitter = require('events'); |
| 69 | +// |
| 70 | +// const emitter = new EventEmitter(); |
| 71 | +// emitter.on("error", error => { |
| 72 | +// console.log(error); // Error: Some Error |
| 73 | +// }); |
| 74 | +// emitter.emit("error", new Error('Some Error')); |
| 75 | + |
| 76 | +// // uncaughtException, 非常规手段 |
| 77 | +// process.on("uncaughtException", (error) => { |
| 78 | +// console.log(error) // ReferenceError: i is not defined |
| 79 | +// }); |
| 80 | +// i++; |
| 81 | + |
| 82 | +// promise unhandledRejection(未监听 uncaughtException时有效) |
| 83 | +// process.on("unhandledRejection", (reason, p) => { |
| 84 | +// console.log(`${reason} ${p}`); // unhandledRejection [object Promise] |
| 85 | +// }); |
| 86 | +// Promise.reject('unhandledRejection'); |
| 87 | + |
| 88 | + |
| 89 | +// 异步导致堆栈丢失 |
| 90 | +const throwError = () => { |
| 91 | + throw new Error(); |
| 92 | +}; |
| 93 | + |
| 94 | +const doSth = () => { |
| 95 | + setImmediate(() => throwError()); |
| 96 | +}; |
| 97 | + |
| 98 | +const main = () => { |
| 99 | + doSth(); |
| 100 | +}; |
| 101 | + |
| 102 | +main(); |
| 103 | +/** |
| 104 | + throw new Error(); |
| 105 | + ^ |
| 106 | +
|
| 107 | + Error |
| 108 | + at throwError (/Users/wangjun/Desktop/practice/opensource/node-every-day/Error:Debugger/Error/index.js:91:11) |
| 109 | + at Immediate.setImmediate (/Users/wangjun/Desktop/practice/opensource/node-every-day/Error:Debugger/Error/index.js:95:24) |
| 110 | + at runCallback (timers.js:705:18) |
| 111 | + at tryOnImmediate (timers.js:676:5) |
| 112 | + at processImmediate (timers.js:658:5) |
| 113 | + * */ |
| 114 | + |
0 commit comments