最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript try-catch not working - Stack Overflow

programmeradmin1浏览0评论

I am new to Java Script. In the below code I am trying to create an error intentionally to learn how try...catch works. But nothing is running.

<html>
<body>
<p id="demo"></p>
<script>
try {
    adddlert("Hi User!");
}
catch(Error e) {
    document.getElementById("demo").innerHTML = e.message;
}
</script>
</body>
</html>

What I am missing?

I am new to Java Script. In the below code I am trying to create an error intentionally to learn how try...catch works. But nothing is running.

<html>
<body>
<p id="demo"></p>
<script>
try {
    adddlert("Hi User!");
}
catch(Error e) {
    document.getElementById("demo").innerHTML = e.message;
}
</script>
</body>
</html>

What I am missing?

Share Improve this question edited Sep 24, 2014 at 12:53 Mithun Satheesh 27.9k14 gold badges79 silver badges102 bronze badges asked Sep 22, 2014 at 13:55 RajuuRajuu 434 bronze badges 1
  • 3 Always have the browser developer console open. – Pointy Commented Sep 22, 2014 at 13:56
Add a ment  | 

2 Answers 2

Reset to default 6

try {
    adddlert("Hi User!");
}
catch(e) {
    document.getElementById("demo").innerHTML = e.message;
}
<html>
<body>
<p id="demo"></p>
</body>
</html>

Syntax Error:

Change your catch (Error e) as catch (e).

More Info: http://www.w3schools./js/js_errors.asp

发布评论

评论列表(0)

  1. 暂无评论