gpt4 book ai didi

javascript - 如何在 Chrome 中捕获 DOMException?

转载 作者:可可西里 更新时间:2023-11-01 02:56:29 26 4
gpt4 key购买 nike

我收到这个错误:

Uncaught (in promise) DOMException: lockOrientation() is not available on this device.
code: 9
message: "lockOrientation() is not available on this device."
name: "NotSupportedError"

当我在 Chrome 中运行以下代码时:

try {
screen.orientation.lock('portrait');
} catch (error) {
// whatever
}

抛出错误是意料之中的事,因为桌面版 Chrome 不支持方向锁定。我想捕获错误,这样它就不会乱扔控制台,但是将它包装在 try...catch block 中似乎不起作用。

为什么我抓不到?我错过了什么吗?

最佳答案

try/catch 在这里不起作用,因为 screen.orientation.lock('portrait'); 实际上返回一个 Promise这是抛出错误。这部分错误表明在 promise 中抛出了异常。

Uncaught (in promise) DOMException: lockOrientation() is not available on this device.

要处理异常,您可以附加一个catch 回调。

screen.orientation.lock('portrait').catch(function(error) {
// whatever
});

关于javascript - 如何在 Chrome 中捕获 DOMException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31509619/

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