JavaScript异常错误处理
2015-08-04 16:25
721 查看
1.异常:
当JavaScript引擎执行JavaScript代码时,发生了错误,导致程序停止运行
2.异常抛出:
当异常产生,并且将这个异常生成一个错误信息
3.异常捕获:
4.Throw语句通过throw语句创建一个自定义错误
当JavaScript引擎执行JavaScript代码时,发生了错误,导致程序停止运行
2.异常抛出:
当异常产生,并且将这个异常生成一个错误信息
3.异常捕获:
try{ 发生异常的代码块 }catch(err){ 错误信息处理 }
<html> <head> <script> var txt=""; function message(){ try{ addlert("welcome") } catch(err){ txt="本页有一个错误异常:"+err.message; alert(txt); } </script> </head> <body> <input type="button" value="button" onclick="message()"> </body> </html>
4.Throw语句通过throw语句创建一个自定义错误
function myFunction() { try { var x=document.getElementById("demo").value; if(x=="") throw "empty"; if(isNaN(x)) throw "not a number"; if(x>10) throw "too high"; if(x<5) throw "too low"; } catch(err) { var y=document.getElementById("mess"); y.innerHTML="Error: " + err + "."; } } </script> <h1>My First JavaScript</h1> <p>Please input a number between 5 and 10:</p> <input id="demo" type="text"> <button type="button" onclick="myFunction()">Test Input</button> <p id="mess"></p>
相关文章推荐
- FLASH CC 2015 CANVAS (二)html中写JS调用flash中的元件、函数、变量
- 【前端学习笔记】深入学习Javascript:DOM和BOM的方法及属性总结
- JavaScript使用encodeURI()和decodeURI()获取字符串值的方法
- 【前端学习笔记】深入学习Javascript:DOM机制
- JavaScript In OA Framework
- sublime格式化代码--html和js
- javascript 函数指针
- javascript将DOM节点添加到文档的方法实例分析
- js javascript 导出table表中数据到Excel中
- WebSocket-SockJS
- WebSocket-SockJS
- js产生随机数教程
- js javascript js控制分页打印,打印分页
- js jsp 时间 日期 控件 插件 简单 实用
- ajax转换成json参数
- 同步按顺序动态加载JS
- JavaScript实现添加及删除事件的方法小结
- Json解析工具的缺陷与可改进点
- javascript运动框架多物体运动---1
- Emberjs路由