The try...catch..finally statement specifies a block of code to try along with a response should an error occur. The try statement contains one or more try blocks, and ends with at least one catch and/or a finally clause.

try...catch..finally语句指定代码块,以在发生错误时尝试与响应一起使用。 try语句包含一个或多个try块,并以至少一个catch和/或finally子句结尾。

try...catch : (try...catch:)

try {throw new Error('my error');
} catch (err) {console.error(err.message);
}// Output: my error

try...finally : (try...finally:)

try {throw new Error('my error');
} finally {console.error('finally');
}// Output: finally

When you don't use a catch statement, the error is not "caught", even though the code in the finally block is executed. Instead, the error will continue to the upper try block (or main block).

当您不使用catch语句时,即使执行了finally块中的代码,也不会“捕获”该错误。 而是,错误将继续到上部try块(或主块)。

try...catch...finally : (try...catch...finally:)

try {throw new Error('my error');
} catch (err) {console.error(err.message);
} finally {console.error('finally');
}// Output:
// my error
// finally

Typical usage:

典型用法:

try {openFile(file);readFile(file)
} catch (err) {console.error(err.message);
} finally {closeFile(file);
}

嵌套try...catch : (Nested try...catch:)

You can also:

你也可以:

  • Nest a try-catch statement inside a try block.

    try-catch语句嵌套在try块内。

You can nest a try...catch statement within a try block. For example, to throw an error upwards:

您可以在try块中嵌套try...catch语句。 例如,向上抛出错误:

try {try {throw new Error('my error');} catch (err) {console.error('inner', err.message);throw err;} finally {console.log('inner finally');}
} catch (err) {console.error('outer', err.message);
}// Output:
// inner my error
// inner finally
// outer my error

翻译自: https://www.freecodecamp.org/news/error-handling-and-try-catch-throw/

JavaScript Try Catch:异常处理说明相关推荐

  1. C++、Java、JavaScript中的异常处理(Exception)

    编程思想之异常处理 什么叫异常处理? 什么叫异常(Exception)?顾名思义就是非正常的情况,出现了不希望出现的意外,异常处理就是遇到这种意外时准备的对策和解决方案.比如您开着一辆劳斯莱斯在公路上 ...

  2. JavaScript try/catch/finally 语句

    实例 在下面的例子中,我们故意在 try 块的代码中写了一个错字. 该实例本应该提醒"欢迎光临!",但弹出的是拼写错误信息. catch 块会捕捉到 try 块中的错误,并执行代码 ...

  3. javascript中的异常处理

    简单来说,可以用如下的语句,来处理javascript中的异常. function message() {     try {         adddlert("Welcome guest ...

  4. Javascript异步操作的异常处理

    try {throw new Error(3) } catch (e) {console.log(e) }try {setTimeout(function () {throw new Error(5) ...

  5. 浅谈Java throw, throws, try catch异常处理

    2019独角兽企业重金招聘Python工程师标准>>> 1.throws关键字通常被应用在声明方法时,用来指定可能抛出的异常.多个异常可以使用逗号隔开.当在主函数中调用该方法时,如果 ...

  6. c++ try...catch异常处理

    c++可以通过throw语句和try...catch语句实现对异常的处理. #include <iostream>using namespace std;int main() {doubl ...

  7. JavaScript 浅析try-catch-finally异常处理是什么意思

    文章目录 try-catch-finally语句 1 作用 2 执行流程 3 常见的错误类型 try-catch-finally语句 1 作用 用于语句发生错误时,捕获错误异常,并做出相应处理. tr ...

  8. SqlServer中常见的语法运用Begin...End\IF条件语句\IF Exists条件语句检测数据是否存在\While循环语句\Case语句执行分之判断\Try...Catch异常处理

    Begin...End Begin...End 是主要用来控制程序块语句中的批处理 语法: Begin {代码块} End 例: go declare @name varchar(50) declar ...

  9. 一个存储过程帮你了解 事务(TRAN)、异常处理(TRY/CATCH)、@@ERROR

    /*  事务:begin transaction开始事务 . commit transaction 提交事务.  rollback transaction 回滚事务. SAVE TRAN 保存事务从本 ...

最新文章

  1. 手把手教你 MongoDB 的安装与详细使用(一)
  2. linux 系统调用会被信号打断的
  3. 目标检测方法系列:R-CNN, SPP, Fast R-CNN, Faster R-CNN, YOLO, SSD
  4. 利用CSS的三列等高布局
  5. linux uvc支持H264格式
  6. 如何开发出优秀的APICloud应用
  7. 递归不行就换动态规划(洛谷P1028题题解,Java语言描述)
  8. 一般试卷的纸张大小是多少_一般试卷的纸张大小
  9. rpm方式安装MySQL-5.6
  10. 《Java从入门到放弃》JavaSE入门篇:JDBC(入门版)
  11. WinForm TabControl美化
  12. h5页面分享一(附源码)
  13. 郭天祥的10天学会51单片机_第九节
  14. 什么是变量?变量的命名规则?变量的分类?(问号三连)
  15. Kryo的基本简单使用
  16. 说说 HWND_TOP 和 HWND_TOPMOST 的区别
  17. idea实现注册登录时使用QQ邮箱激活功能
  18. Mysql coalesce()函数认识和用法
  19. OpenCV各版本差异与演化,从1.x到4.0
  20. 12槽10极分数槽集中绕组永磁电机结构讲解

热门文章

  1. 上机 文件读写器 c# 1614998685
  2. nginx配置静态资源允许跨域访问
  3. 教你如何完全解析Kotlin中的注解
  4. 【RUST官方语言中文翻译】前言
  5. Winform开发框架中工作流模块之申请单草稿处理
  6. CAICT:2015年全球云计算市场规模522亿美元
  7. JasperReports项目中的应用
  8. NSOperation的使用细节 [3]
  9. LB Cluster 之一:集群及LVS基础理论详解
  10. CodeBlocks问题解决