-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第81天 在js中怎么捕获异常?写出来看看?应该在哪些场景下采用呢? #578
Comments
try {
...
throw ...
...
} catch (err) {
...
} finally {
...
}
该在什么场景下用?所有你不想让用户接手的错误处理都应该用。 |
window.onerror = function(message, source, lineno, colno, error) { ... } 可以在全局顶层监听未捕获的错误 |
通常在以下几点使用:
async function requestData() {
try {
if (this.loading) return
this.loading = true
await api.getData()
...
} catch (err) {
console.error(err)
} finally {
this.loading = false
}
} |
(1) js在运行过程中,一旦发生错误,程序就中止执行了;这极大的影响了用户体验。 (2) 注意:语法错误无效,比如输入变量时多写了符合:等 (3) (4) 使用场景很多,只要是任何你认为可能报错的地方时都可以使用 try{
throw new Error('err');
}catch(e){
console.log(e)
} |
一般都是 |
第81天 在js中怎么捕获异常?写出来看看?应该在哪些场景下采用呢?
The text was updated successfully, but these errors were encountered: