为什么 Mozilla 认为条件捕获块是非标准的?
Why does Mozilla consider Conditional catch-blocks non-standard?
我在 Mozilla 的文档中看到 try...catch 条件是
"non-standard and is not on a standards track. Do not use it on
production"
但我不明白为什么。
而且我想知道在可以为每种情况创建多个 response.status 的情况下,我如何才能做出此声明。例如,这是我现在的代码
class BookController {
async store ({ request, response }) {
try {
const values = { ...request.all(), user_id: request.user_id }
const validatedValues = await importValidate.validate(values, rules, messages)
return Book.create(validatedValues)
} catch (e) {
response.status(409).json(e)
}
}
而且我需要它是每种情况的倍数 response.status,对于基于我的规则的每个响应。
我目前正在使用 JS、Node、Adonis,在此验证过程中,Indicative/Validator
如您阅读的文档所述:
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
在这种情况下,您最好定义自己的条件语句
而不是
try {
myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
使用
try {
myroutine(); // may throw three types of exceptions
} catch (e) {
if (e instanceof TypeError) {
// statements to handle TypeError exceptions
} else if (e instanceof RangeError) {
// statements to handle RangeError exceptions
} else if (e instanceof EvalError) {
// statements to handle EvalError exceptions
} else {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
}
通过这种方式,您仍将单独处理所有异常,但在单个 catch 语句中
我在 Mozilla 的文档中看到 try...catch 条件是
"non-standard and is not on a standards track. Do not use it on production"
但我不明白为什么。
而且我想知道在可以为每种情况创建多个 response.status 的情况下,我如何才能做出此声明。例如,这是我现在的代码
class BookController {
async store ({ request, response }) {
try {
const values = { ...request.all(), user_id: request.user_id }
const validatedValues = await importValidate.validate(values, rules, messages)
return Book.create(validatedValues)
} catch (e) {
response.status(409).json(e)
}
}
而且我需要它是每种情况的倍数 response.status,对于基于我的规则的每个响应。
我目前正在使用 JS、Node、Adonis,在此验证过程中,Indicative/Validator
如您阅读的文档所述:
Non-standard This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
在这种情况下,您最好定义自己的条件语句
而不是
try {
myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
使用
try {
myroutine(); // may throw three types of exceptions
} catch (e) {
if (e instanceof TypeError) {
// statements to handle TypeError exceptions
} else if (e instanceof RangeError) {
// statements to handle RangeError exceptions
} else if (e instanceof EvalError) {
// statements to handle EvalError exceptions
} else {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
}
通过这种方式,您仍将单独处理所有异常,但在单个 catch 语句中