To learn try-catch

A piece of code will be when something goes wrong, "dead" (stop execution) and will print out an exception in the console.
try..catch, it will catch the exception while not making the code execution to stop but could do with some more reasonable operation, provide exception handling.

basic structure

try {
   ... 尝试执行的代码 ...
} catch(e) {
    // 如果发生异常,跳到这里
   ... 异常处理 ...
} finally {
   ... 最终会执行的代码 ...
}

try

try..catch only works for runtime errors

try-catch can not catch the exception (parse-time) appears read the code, the runtime can be captured (Runtime) throws an exception;

try..catch works synchronously

try-catch perform synchronization
words, the try of the asynchronous operation, will not be captured catch; asynchronous operation, a try-catch capture anomaly;

try {
  setTimeout(function() {
    noSuchVariable; // 代码在这里停止执行
  }, 1000);
} catch (e) {
  alert( "won't work" );
}

setTimeout(function() {
  try {
    noSuchVariable; // try..catch 处理异常!
  } catch (e) {
    alert( "error is caught here!" );
  }
}, 1000);

Error

  • Throw an exception source object that contains specific information
  • name: constructor name, exception name
  • message: constructor parameters, abnormal details text description
  • Stack (non-standard): the current call stack, comprising a trigger string nested exception call sequence.

  • Custom thrown
    • throw new Error(message);
  • An exception is thrown again
    • catch catch only known exception, and the exception re-thrown unknown (instructive)
function readData() {
  let json = '{ "age": 30 }';

  try {
    // ...
    blabla(); // 预料之外的异常
  } catch (e) {
    if (e.name == "SyntaxError") { // 已知,具体异常
        alert( "JSON Error: " + e.message );
    } else {
        throw e; // rethrow (*)重新抛出(不知道如何处理它)
    }
  }
}

// 最外层捕获,将异常抛到js运行时环境
try {
  readData();
} catch (e) {
  alert( "External catch got: " + e ); // 捕获到!
}

finally (will eventually execute)

Variables are local inside try..catch..finally

let, const variables defined only in the block-level access scope, as will be appreciated, the try and catch defined variables can not catch, and finally accessed.

finally and return

try..catch in any way the end of execution (including return, throwing an exception), finally will be executed

Environment-specific (global exception)

  • nodejs
    • process.on('uncaughtException')
  • browser
    • window.onerror = function(message, url, line, col, error) {}
      • message: exception information;
      • url: URL code abnormal occurrence;
      • line, col: Error code line number and column number;
      • error: exception object;
  • application
    • Registration abnormal recording service, the exception information synchronized to the server, log analysis, identify problems and resolve;

[Sources and Extended]

https://mp.weixin.qq.com/s/jHSk4UeNmQ1ih_F5vs0jdw

https://github.com/BooheeFE/weekly/issues

https://javascript.info/js

Guess you like

Origin www.cnblogs.com/malq/p/11038406.html