全局异常处理:
示例:
function test1() {
try {
console.log('test1')
test2( )
} catch ( error ) {
console.log( 'catch in test1', error )
}
}
function test2( ) {
throw new Error( 'err in test2' )
console.log( 'test2' )
test3( )
}
function test3( ) {
consloe.log(' test3 ')
}
这样就可以全局捕获异常:
async function test1( ) {
try {
await test2 ( )
} catch (error ) {
console.log('catch in test1', error)
}
}
function test2( ) {
return new Promise( ( resolve, reject ) => ) {
setTimeout( ( ) => {
reject( 'err in test2' )
},1000 )
}
}
test1( )