[英]How to catch DOMException in Chrome?
我收到此錯誤:
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
塊似乎不起作用。
為什么我不能抓住它? 我錯過了什么嗎?
try/catch
在這里不起作用,因為screen.orientation.lock('portrait');
實際上返回一個拋出錯誤的Promise 。 這部分錯誤顯示了promise中拋出的異常。
未捕獲(在promise中)DOMException:此設備上沒有lockOrientation()。
要處理異常,您可以附加catch
回調。
screen.orientation.lock('portrait').catch(function(error) {
// whatever
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.