什么时候应该在 JavaScript 中使用 try/catch? [英] When should you use try/catch in JavaScript?

查看:41
本文介绍了什么时候应该在 JavaScript 中使用 try/catch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 JavaScript 开发普通的 Web 应用程序时,通常不需要 try/catch 语句.JavaScript 中没有 检查异常、文件 IO 或数据库连接.

When I'm developing normal web application with JavaScript, the try/catch statement is not needed usually. There's no checked exception, File IO or database connection in JavaScript.

try/catch 语句在 JavaScript 中有用吗?我什么时候可以使用它?

Is try/catch statement useful in JavaScript? When can I use it?

推荐答案

每当您运行的代码可能引发异常时,请使用它.请记住,你可以抛出你自己的错误——我使用的大部分try...catch东西都是为了捕捉我自己的异常.

Use it whenever code you are running might throw an exception. Remember that you can throw your own errors — most of the try…catch stuff I use is for catching my own exceptions.

这篇关于什么时候应该在 JavaScript 中使用 try/catch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆