复制代码 代码如下: //Throw an Error object. try{ throw new Error("Message in Error Object"); }catch(e){ console.log(e);//Error: Message in Error Object } try{ throw "Raw Message"; }catch(message){ console.log(message);//Raw Message console.log(typeof message);//string } try{ throw 42; }catch(code){ console.log(code);//42 console.log(typeof code);//number }
与Java语言一样,如果异常没有被任何catch语句所捕捉,那么该异常会最终抛给用户:
复制代码 代码如下: try{ //throw new Error("test error");//Error will be thrown. Error: test error }finally{ } try{ throw 42;//Error will be thrown. Error: 42 }finally{ }