javascript try...catch 语句如何工作 [英] How javascript try...catch statement works
问题描述
我正在尝试在 browsermob 中测试某些输入字段是否有效.我正在尝试使用我以前从未使用过的 try...catch 语句.我知道表格是:
I am trying to test in browsermob if certain input field work or not. I am attempting to use a try...catch statement which I have never used before. I know that the form is:
try {
//some code
} catch (){
//some error code
};
catch 语句后的括号中究竟应该放什么?当我尝试使用该语句时,无论它是否不是错误,它都会通过 catch 语句运行所有内容.我做错了什么?
What exactly is supposed to be put in the parenthesis after the catch statement? When I try to use the statement it runs everything through the catch statement no matter if it is not an error. What am I doing wrong?
推荐答案
See the "try...catch
statement" guide on MDN.
简而言之,try/catch 用于处理异常(使用 throw
语句抛出").try/catch 的语法是:
In short, try/catch is used to handle exceptions (which are "thrown" using the throw
statement). The syntax for try/catch is:
try {
// Code
} catch (varName) { // Optional
// If exception thrown in try block,
// execute this block
} finally { // Optional
// Execute this block after
// try or after catch clause
// (i.e. this is *always* called)
}
varName
仅适用于 catch
块的范围.它指的是抛出的异常对象(可以是任何类型的对象,例如 String
,但通常是 Error
object).
varName
is available to the scope of the catch
block only. It refers to the exception object which was thrown (which could be any type of object, e.g. a String
, but is usually an Error
object).
这篇关于javascript try...catch 语句如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!