使用try{...}catch(){} 方式,例子如下: function showAlarm(strObject){ try{ svgElement = document.getElementById(strObject); svgElement.setAttribute("visibility","visible"); setTimeout("hideAlarm()",3000);//3秒后结束报警 }catch(ex){ alert("函数showAlarm()发生异常:"+ex.description); }