当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript Error 对象 错误处理

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 javascript Error 对象 错误处理


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 368 ::
收藏到网摘: n/a

javascript常见error对象处理,错误代码集合 Error对象
Property:
name: 错误名
number: 错误号
description: 描述
message: 错误信息,多同description
FF Only 属性
fileName: 错误发生的文件
stack: 错误发生时的调用堆栈

Constructor:
Error(){
this(0,"")}
Error(description){
this(0,description)}
Error(number,description){
....}
构造函数参数不带name,是因为Error对象的name对应于它的来源:
EvalError: 错误发生在eval()中
SyntaxError: 语法错误,错误发生在eval()中,因为其它点发生SyntaxError会无法通过解释器
RangeError: 数值超出范围
ReferenceError: 引用不可用
TypeError: 变量类型不是预期的
URIError: 错误发生在encodeURI()或decodeURI()中
抛出Error:
throw new Error(0,"Error Demo");
new Error可省略:
throw("Error Demo");
捕获Error:
try catch finally语句:
try{
..可能错误的语句..}
catch(e){
..错误发生后的处理..}
finally{
..完成后执行的语句块..}
finally不是必须的
如果嵌套,两个catch不要使用同一个参数名,以免覆盖
传入的参数是一个Error对象,可以从中得到错误信息
FF支持一个try多个catch,因为Js为弱类型不推荐使用
window.onerror错误捕获:
window.onerror=function(Msg,Url,Num){}
onerror事件会传给回调函数3个默认参数
Msg: 错误信息
Url: 发生错误的文件的Url
Num: 错误发生位置的行号
window.onerror还能处理SyntaxError,比try catch更强大。
但是onerror属于bom,所以各浏览器厂家对其的支持都不同。
如IE发生error时,正常的代码会继续执行;在FF中,代码将结束;Safari只支持Image的onerror事件处理。
Image.onerror
onerror还可以应用于其它HTMLElement上,最常见的是<img />元素
<img src="sample.jpg" onerror="javascript:alert('图像载入错误');" />
处理Error:
判断错误类型:
catch(e){
if(e.name=="RangeError")
alert("错误提示");}

catch(e){
if(e instanceof TypeError)
alert("错误提示");}