js的异常处理 try...catch
来源:互联网 发布:花都金蝶软件 编辑:程序博客网 时间:2024/05/18 13:08
1,try…catch…
try 语句测试代码块的错误。
catch 语句处理错误。
throw 语句创建自定义错误。
try { //在这里运行代码 }catch(err) { //在这里处理错误 }
我们故意在 try 块的代码中写一个错字。
catch 块会捕捉到 try 块中的错误,并执行代码来处理它。
本例检测输入变量的值。如果值是错误的,会抛出一个异常(错误)。catch 会捕捉到这个错误,并显示一段自定义的错误消息:
<script>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>
阅读全文
0 0
- js的异常处理 try...catch
- JS中的异常处理(try.catch)
- js try{}catch(err){} 异常处理
- js中的异常处理try...catch
- try catch 异常处理
- try-catch异常处理
- try catch 异常处理
- 异常处理(try catch)
- 异常处理@try catch
- 异常处理try.....catch
- try catch异常处理
- try catch异常处理
- 异常处理try catch
- 异常处理的try-catch-finally语句
- php 的异常处理try catch
- 黑马程序员:try-catch异常的处理
- php的异常处理 try catch
- try catch 不到异常的处理
- 关于在MyEclipse中启动项目报错找不到ThreadLocalErrorLogAppender 的解决办法
- springMvc注解@RequestParam用法解析
- C#多线程和线程池
- Jena的简单理解和一个例子
- 基础练习 BASIC-1 闰年判断(c语言)
- js的异常处理 try...catch
- PXE 自动化运维
- Vue.js——60分钟快速入门
- 四次挥手中TIME_WAIT,CLOSE_WAIT,FIN_WAIT_1,FIN_WAIT_2状态浅析
- 设计模式(二十一)------23种设计模式(13):迭代器模式
- \/Date(1513130414572)\/ 格式转换
- 江哥案之后,江妈妈应该怎样生活
- 开源地图导航引擎:graphhopper
- php实现换一换功能