LMLPHP后院

JavaScript try catch 语句技术

maybe yes 发表于 2017-06-04 17:49

在 js 中,很少人使用 try catch 语言,其实偶尔还是需要的。

常用的场景,比如使用 JSON.parse() 时。

try {
  throw new Error("Whoops");
} catch (e) {
  if (e instanceof EvalError) {
    console.log(e.name + ': ' + e.message);
  } else if (e instanceof RangeError) {
    console.log(e.name + ': ' + e.message);
  }
}
2024-04-25 09:51:35 1714009895 0.005709