JS中的异常exception

js提供了一套异常处理机制。异常是干扰程序的正常流程的不寻常事故,当发生这样的事故时,你的程序应该抛出一个异常

           try_it() {
                try {
                    console.log(add("1",2))
                }catch(e){
                    console.log(e.name + ':' + e.message)
                }

            },
            add(a,b) {
                if(typeof a != "number" || typeof b != "number"){
                    throw{
                        name:'typeErr',
                        message:'you should enter a number'
                    }
                }
                return a+b
            }
       try_it()
原文地址:https://www.cnblogs.com/cnundefined/p/7000665.html