(鉴于)膳绫擎所谈到的所有工尴尬刁难大年夜多半仁攀来讲都是小菜一碟,那么就让我们来谈一些不为人所知的细节。
try代码块后面不必紧跟着catch,但(此种情况下)厥后必须紧跟着finally。这意味着我们可以应用三种不合情势的try语句:
- try...catch
- try...finally
- try...catch...finally
- try {
- try {
- throw new Error('Nested error.'); // The error thrown here will be caught by its own `catch` clause
- } catch (nestedErr) {
- console.log('Nested catch'); // This runs
- }
- } catch (err) {
- console.log('This will not run.');
- }
你甚至还可以在catch和finally代码块中嵌套try语句:
- try {
- throw new Error('First error');
- } catch (err) {
- console.log('First catch running');
- try {
- throw new Error('Second error');
- } catch (nestedErr) {
- console.log('Second catch running.');
- }
- }
- try {
- console.log('The try block is running...');
- } finally {
- try {
- throw new Error('Error inside finally.');
- } catch (err) {
- console.log('Caught an error inside the finally block.');
- }
- }
还有很重要的一点值得留意,那就是我们甚至可以大年夜可不必抛出Error对象。尽管这看起来异常cool且异常自由,但实际并非如斯,尤其是对开辟第三方库的开辟者来说,因为他们必须处理用户(应用库的开辟者)的代码。因为缺乏标准,他们并不克不及把控用户的行动。你不克不及信赖用户并简单的抛出一个Error对象,因为他们不必定会那么做而是仅仅抛出一个字符串或者数字(鬼知道用户会抛出什么)。这也使得处理须要的客栈跟踪和其他有意义的元数据变得加倍艰苦。
起首,让我们来看看当断言掉败时抛出的AssertionError的构造函数:
假设有以下代码:
- function runWithoutThrowing(func) {
- try {
- func();
- } catch (e) {
- console.log('There was an error, but I will not throw it.');
- console.log('The error\'s message was: ' + e.message)
- }
- }
推荐阅读
近些年来,深度卷积神经收集(DCNN)在图像分类和辨认上取得了很明显的进步。回想大年夜 2014 到 2016 这两年多的时光,先后出现出了 R-CNN,Fast R-CNN, Faster R-CNN, ION, HyperNet, SD>>>详细阅读
地址:http://www.17bianji.com/lsqh/34953.html
1/2 1