How to catch DOMException in Chrome?

痴心易碎 提交于 2019-12-22 04:12:26

问题


I get this error:

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

when I run the following code in Chrome:

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

The fact that the error is being thrown is expected, since Desktop Chrome doesn't support orientation locking. I'd like to catch the error so it doesn't litter the console, but wrapping it in a try...catch block doesn't seem to work.

Why can't I catch it? Am I missing something?


回答1:


try/catch does not work here, because screen.orientation.lock('portrait'); actually returns a Promise which is throwing the error. This part of the error shows the exception is thrown in the promise.

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

To handle the exception, you can attach a catch callback.

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


来源:https://stackoverflow.com/questions/31509619/how-to-catch-domexception-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!