gpt4 book ai didi

javascript - 为什么 Mozilla 认为条件捕获 block 是非标准的?

转载 作者:行者123 更新时间:2023-11-30 09:10:44 24 4
gpt4 key购买 nike

我在这里阅读 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/验证器

最佳答案

正如您阅读的文档所解释的那样:

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 语句中

关于javascript - 为什么 Mozilla 认为条件捕获 block 是非标准的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59324187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com