[英]How javascript try…catch statement works
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. 我试图使用一个我从未使用过的try ... catch语句。 I know that the form is: 我知道表格是:
try {
//some code
} catch (){
//some error code
};
What exactly is supposed to be put in the parenthesis after the catch statement? 在catch语句之后应该在括号中放置什么? When I try to use the statement it runs everything through the catch statement no matter if it is not an error. 当我尝试使用该语句时,无论是否错误,它都会通过catch语句运行所有内容。 What am I doing wrong? 我究竟做错了什么?
See the “ try...catch
statement” guide on MDN . 请参阅MDN上的“ try...catch
语句”指南 。
In short, try/catch is used to handle exceptions (which are "thrown" using the throw
statement). 简而言之,try / catch用于处理异常(使用throw
语句“抛出”)。 The syntax for try/catch is: try / catch的语法是:
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
is available to the scope of the catch
block only. varName
仅可用于catch
块的范围。 It refers to the exception object which was thrown (which could be any type of object, eg a String
, but is usually an Error
object ). 它引用了抛出的异常对象(可以是任何类型的对象,例如String
,但通常是Error
对象 )。
The try catch statement is used to detected for exceptions/errors that are raised inside the try
-block. try catch语句用于检测try
-block中引发的异常/错误。 In the catch block you can then react on this exceptional behavior and try to resolve it or get to a safe state. 在catch块中,您可以对此异常行为做出反应并尝试解决它或进入安全状态。
You got the statement almost right: 你的声明几乎是正确的:
try {
// code that may fail with error/exception
} catch (e) { // e represents the exception/error object
// react
}
Consider the following examples: 请考虑以下示例:
try {
var x = parseInt("xxx");
if(isNaN(x)){
throw new Error("Not a number");
}
} catch (e) { // e represents the exception/error object
alert(e);
}
try {
// some code
if(!condition){
throw new Error("Something went wrong!");
}
} catch (e) { // e represents the exception/error object
alert(e);
}
According to ECMAScript
specifications, 根据ECMAScript
规范,
try {
// Code
} catch (varName) { // optional if 'finally' block is present.
if (condition) { // eg. (varName instanceof URIError)
// Condition (Type) specific error handling
}
else {
// Generic error handling
}
} finally { // Optional if 'catch' block is present.
// Execute this block after
// try or after catch clause
// (i.e. this is *always* called)
}
the stuff inside try {...} is what you want to execute. try {...}里面的东西就是你想要执行的东西。 The stuff in catch() { ... } is what you want to execute if you get any javascript errors from anything executed in the try {...} catch(){...}中的东西是你想要执行的东西,如果你从try {...}中执行的任何东西得到任何javascript错误
catch {...} only executes if there is a javascript error in the try {...} block. catch {...}仅在try {...}块中出现javascript错误时执行。 You can find out what the error is by doing for example this: 您可以通过以下方式找出错误:
try {
// do something
} catch (err) {
alert(err);
}
the code that is likely to throw an exception goes into try { }
, The code to be run when an exception is thrown, comes into catch() { }
. 可能引发异常的代码进入try { }
,抛出异常时要运行的代码进入catch() { }
。 In catch() you can specify which exceptions you want to catch, and in which automatic variables to put it. 在catch()中,您可以指定要捕获的异常以及放置它的自动变量。 finally { }
is always run, regardless whether exception was thrown or not. 无论是否抛出异常, finally { }
始终运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.