how2j.cn


工具版本兼容问题
JavaScript提供了一种try catch的错误处理机制,当有错误抛出的时候,可以catch住。

示例 1 : 调用不存在的函数   
示例 2 : try catch   

示例 1 :

调用不存在的函数

在本例中故意调用不存在的函数f2, 因为函数f2不存在,所以会出错,并且会中断执行。 导致后面的代码也不会执行了。
"); window.frames["iframe_show1114"].document.write(decodeHtml($("textarea#stepcodeTextarea1114").val())); window.frames["iframe_show1114"].document.close(); $(window.frames["iframe_show1114"]).load(function(){ $("#iframe_show1114").height($("#iframe_show1114").contents().find("body").height()+showittryitheight); }); $("#iframe_show1114").height($("#iframe_show1114").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write("试图调用不存在的函数f2()就会报错,并且导致javascript中止执行,后面的代码也不会执行了"); function f1(){ //函数f1是存在的 } f2(); //调用不存在的函数f2(); document.write("剩下的代码"); </script>
<script>
document.write("试图调用不存在的函数f2()就会报错,并且导致javascript中止执行,后面的代码也不会执行了"); 

function f1(){
  //函数f1是存在的
}
f2();  //调用不存在的函数f2();
document.write("剩下的代码"); 
</script>
"); window.frames["iframe1114"].document.write(decodeHtml(code1114)); window.frames["iframe1114"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1114"]).load(function(){ $("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight); }); $("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight); alreadyWriteCode1114 = code1114; $("#rendering1114").hide(); $("#rendered1114").show(); } var tRereshRetry2DemoPanel1114 = setInterval(rereshRetry2DemoPanel1114,1000); var binded1114 = false; $("textarea#stepcodeTextarea1114").keyup(function(){ if(!binded1114){ $(window).bind('beforeunload',function(){ binded1114 = true; return "xxxx"; }); } var newCode = $(this).val() code1114 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1114!=newCode){ // window.frames["iframe1114"].document.write("
"); // window.frames["iframe1114"].document.write(decodeHtml($("textarea#stepcodeTextarea1114").val())); // window.frames["iframe1114"].document.close(); // $(window.frames["iframe1114"]).load(function(){ // $("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight); // }); // code1114 = newCode; // } }); $(".tryButton1114").click(function(){ $("#tryDiv1114").show(); $("#stepcodeTextarea1114").focus(); $("#stepcodeTextarea1114").height(200); $("#iframe1114").height(0); window.frames["iframe1114"].document.write("
"); window.frames["iframe1114"].document.write(decodeHtml($("textarea#stepcodeTextarea1114").val())); window.frames["iframe1114"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1114"]).load(function(){ $("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight); }); $("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1114.focus(); editor1114.setSize(null, "250"); $("#rendering1114").hide(); $("#rendered1114").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1114 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1114"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1114.on("change",function(doc){ if(!binded1114){ $(window).bind('beforeunload',function(){ binded1114 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1114 = newCode; $("textarea#stepcodeTextarea1114").val(newCode); if(alreadyWriteCode1114!=code1114){ lastModifedTime1114 = new Date().getTime(); $("#rendering1114").show(); $("#rendered1114").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1114 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1114.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1114").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

try catch

对f2()调用进行try catch的处理。 捕捉到了该方法未定义的错误。并打印出来。
后续的代码,也就可以继续执行了。
"); window.frames["iframe_show1115"].document.write(decodeHtml($("textarea#stepcodeTextarea1115").val())); window.frames["iframe_show1115"].document.close(); $(window.frames["iframe_show1115"]).load(function(){ $("#iframe_show1115").height($("#iframe_show1115").contents().find("body").height()+showittryitheight); }); $("#iframe_show1115").height($("#iframe_show1115").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function f1(){ //函数f1是存在的 } try{ document.write("试图调用不存在的函数f2()<br>"); f2(); //调用不存在的函数f2(); } catch(err){ document.write("捕捉到错误产生:"); document.write(err.message); } document.write("<p>因为错误被捕捉了,所以后续的代码能够继续执行</p>"); </script>
<script>

function f1(){
  //函数f1是存在的
}
try{
   document.write("试图调用不存在的函数f2()<br>"); 
    f2();  //调用不存在的函数f2();
}
catch(err){
   document.write("捕捉到错误产生:"); 
    document.write(err.message);
}

document.write("<p>因为错误被捕捉了,所以后续的代码能够继续执行</p>"); 

</script>
"); window.frames["iframe1115"].document.write(decodeHtml(code1115)); window.frames["iframe1115"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1115"]).load(function(){ $("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight); }); $("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight); alreadyWriteCode1115 = code1115; $("#rendering1115").hide(); $("#rendered1115").show(); } var tRereshRetry2DemoPanel1115 = setInterval(rereshRetry2DemoPanel1115,1000); var binded1115 = false; $("textarea#stepcodeTextarea1115").keyup(function(){ if(!binded1115){ $(window).bind('beforeunload',function(){ binded1115 = true; return "xxxx"; }); } var newCode = $(this).val() code1115 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1115!=newCode){ // window.frames["iframe1115"].document.write("
"); // window.frames["iframe1115"].document.write(decodeHtml($("textarea#stepcodeTextarea1115").val())); // window.frames["iframe1115"].document.close(); // $(window.frames["iframe1115"]).load(function(){ // $("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight); // }); // code1115 = newCode; // } }); $(".tryButton1115").click(function(){ $("#tryDiv1115").show(); $("#stepcodeTextarea1115").focus(); $("#stepcodeTextarea1115").height(200); $("#iframe1115").height(0); window.frames["iframe1115"].document.write("
"); window.frames["iframe1115"].document.write(decodeHtml($("textarea#stepcodeTextarea1115").val())); window.frames["iframe1115"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1115"]).load(function(){ $("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight); }); $("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1115.focus(); editor1115.setSize(null, "250"); $("#rendering1115").hide(); $("#rendered1115").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1115 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1115"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1115.on("change",function(doc){ if(!binded1115){ $(window).bind('beforeunload',function(){ binded1115 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1115 = newCode; $("textarea#stepcodeTextarea1115").val(newCode); if(alreadyWriteCode1115!=code1115){ lastModifedTime1115 = new Date().getTime(); $("#rendering1115").show(); $("#rendered1115").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1115 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1115.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1115").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JavaScript-错误处理 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图