- Objective: - Breadcrumb: # 概念阐释 ## 语义 构建一个Error函数,同时不影响后面的代码继续运行。 ## 语法 ```js try { }catch(){ } ``` # 实例 #### 当用户输入的信息有误时,抛出错误,但继续运行后面的代码 ```js try { throw ('name is too short'); }catch(e){ console.log(e); } console.log('这行代码将继续运行') ``` #### 捕获外部资源数据中的Error - 这在实际应用中用来检查代码非常有用 - 本来就有错误的代码不需要使用throw来创建Error了 ```js function capAllElements(arr){ try { arr.forEach((el, index, array) => { array[index] = el.toUpperCase(); });} catch(e){ console.log(e); }; } capAllElements('Incorrect argument'); ``` # 相关内容 # 问题 # 问题答案 # 参考资料 [try...catch-MDN](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/try...catch)