其他分享
首页 > 其他分享> > 前端系列二十六:异常处理

前端系列二十六:异常处理

作者:互联网

全局异常处理:

示例:

        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( )

标签:test1,二十六,test2,系列,log,function,前端,catch,console
来源: https://blog.csdn.net/WebYanXin/article/details/122251918