为什么输出不显示任何东西

时间:2017-09-27 19:06:01

标签: javascript html

输出什么都没有,我找不到的代码有什么问题

<!DOCTYPE text>
<html>
<head>
     <title>Exceptional Handling </title>

     <script type="text/javascript">
       var a = 200;
       var b = 10;

       try {
         if(b == 0) {
           throw("Divide by zero error");
         } else {
           var c= a / b;
           document.write(c);
         }
       } catch(e) {
         alert("Error"+e);
       } finally() {
         document.write("Anything will be executed here");
       }
</script>

输出什么都没有,我找不到的代码有什么问题

3 个答案:

答案 0 :(得分:0)

您应该使用finally,而不是finally()

&#13;
&#13;
<!DOCTYPE text>
<html>
<head>
     <title>Exceptional Handling </title>

     <script type="text/javascript">
       var a = 200;
       var b = 10;

       try {
         if(b == 0) {
           throw("Divide by zero error");
         } else {
           var c= a / b;
           document.write(c);
         }
       } catch(e) {
         alert("Error"+e);
       } finally {
         document.write("Anything will be executed here");
       }
     </script>
</head>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

当您查看浏览器控制台时,您会看到它引发了错误:

  

SyntaxError:missing {before finally block

错误消息可能不会指向您所做的确切错误,但阅读有关try...catch的文档会这样做。 finally没有参数,因此删除后面的(),您的代码就可以运行。

答案 2 :(得分:0)

问题似乎是在try..catch..finally声明中,最后&#39;没有参数。我还在警报文本中添加了空格和分号,并在finally语句中添加了空格和分号,以使输出更加愉快。我希望这会有所帮助。

<script>
// For clarity, result is 20. Changing "var b" to 0 throws the error alert.
// 'finally' statement is always executed regardless.

var a = 200;
var b = 10;

try{
  if(b == 0){
throw("Divide by zero error");
} else{
  var c = a/b;
  document.write(c);
  }
}

catch(e){
  alert("Error: "+e);
}

finally{
  document.write("<br>Anything will be executed here");
}
</script>