网页技巧:妥善处理JavaScript中的错误(2)

2008-02-23 08:08:43来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


Address: <input id="contactNumber" name="contactNumber" type="text"><br>
<input type="button" value="Submit" onclick="doIt();">
</form></body></html>

它提示了以下信息,但是finally块保证了窗体的提交——不管有什么错误发生。

An unexpected error has occurred.
Please contact the administrator.
'document.forms.0.firstName.value' is null or not an object

单个catch块可以处理所有问题,但是多个catch语句可以被用来处理特定的错误。这个问题在下个部分会涉及到。

用try/catch语句可以很容易地处理不可预见的错误。在某些情况下,可能你想以不同的方式处理错误,JavaScript提供了throw语句。它的句法是很基本的——throw后面紧跟要发生的异常。这就使得你能够定义和引发自定义的异常。列表E中的代码创建了一个缺失值的异常,并且它是生成的。

列表E

<html><head>
<title>JS Test</title>
<script type="text/javascript">
function MissingValueException (errMsg) {
this.message = errMsg;
this.name="MissingValueException";
}
function doIt() {
try {
if (document.forms[0].fullName.value == '') {
noNameException = new MissingValueException("First name is missing.");
throw noNameException;
} else {
document.forms[0].submit();
}
} catch(e) {
document.write("An unexpected error has occurred.<br>");
document.write("Please contact the administrator.<br>");
document.write(e.message);
} finally {
document.forms[0].submit();
}
return 0; }
</script></head><body>
<form id="frmTest">
Name: <input id="fullName" name="fullName" type="text"><br>
Address: <input id="contactNumber" name="contactNumber" type="text"><br>
<input type="button" value="Submit" onclick="doIt();">
</form></body></html>

它将会显示以下信息(如果在字段中没有值输入的话):

An unexpected error has occurred.
Please contact the administrator.
First name is missing.

除此之外,你还可以用运算符instanceof来确定异常的类型,从而做出反应。列表F中的代码会检查异常对象的类型并相应地显示有关数据。

列表F

<html>
<head>
<title>JS Test</title>
<script type="text/javascript">
function MissingValueException (errMsg) {
this.message = errMsg;
this.name="MissingValueException";
}
function doIt() {
try {
if (document.forms[0].fullName.value == '') {
noNameException = new MissingValueException("First name is missing.");
throw noNameException;
}
} catch(e) {
if (e instanceofMissingValueException) {
document.write(e.message "<br>");
document.write("Please contact the administrator.<br><br>");
} else {
document.write("An unexpected error has occurred.<br>");
document.write("Please contact the administrator.<br>");
document.write(e.message);
} } finally {
document.forms[0].submit();
} return 0; }
</script></head><body>
<form id="frmTest">
Name: <input id="fullName" name="fullName" type="text"><br>
Address: <input id="contactNumber" name="contactNumber" type="text"><br>
<input type="button" value="Submit" onclick="doIt();">
</form></body></html>

运算符instanceof可以同标准错误一起使用。JavaScript定义了以下标准的JavaScript错误类型:

EvalError:表明全局的eval函数使用错误。
RangeError:说明一个数值超过了所允许的值的范围。
ReferenceError:发现了一个非法的引用。
SyntaxError:发生了一个句法分析错误。
TypeError:一个操作数的实际类型与所预期的类型不同。
URIError:其中一个全局URI函数(编码URI或解码URI)使用错误。
列表G中的代码在一个catch语句中采用了TypeError类型。由于在引用字段名(document)的行中多了一个d,结果发生了一个打字错误(ddocument)。

列表G

<html><head>
<title>JS Test</title>
<script type="text/javascript">
function MissingValueException (errMsg) {
this.message = errMsg;
this.name="MissingValueException";
}
function doIt() {
try {
if (ddocument.forms[0].fullName.value == '') {
noNameException = new MissingValueException("First name is missing.");
throw noNameException;
}
} catch(e) {
if (e instanceofTypeError) {
document.write("Reference error while accessing First Name field.<br><br>");
document.write("Please contact the administrator.<br><br>");
document.write(e.message);
} else {
document.write("An unexpected error has occurred.<br><br>");
document.write("Please contact the administrator.<br><br>");
document.write(e.message);
} } finally {
document.forms[0].submit();
} return 0; }
</script></head>
<body><form id="frmTest">
Name: <input id="fullName" name="fullName" type="text"><br>
Address: <input id="contactNumber" name="contactNumber" type="text"><br>
<input type="button" value="Submit" onclick="doIt();">

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:用javascript 转换外部链接样式

下一篇:Javascript XML实现分页的实例