RSS

异常处理语句

24 Jan

JavaScript中创建了多个用于处理错误的对象。这些对象可以分为JavaScript异常对象和DOM异常对象两大类。 JavaScript异常对象有以下几种: 1) Error: 普通异常 2) EvalError: 当不正确使用eval()时抛出的异常 3) RangeError: 当数字超出合法范围时抛出的异常 4) ReferenceError: 当读取不存在的变量时抛出的异常 5) SyntaxError: 当语法错误时抛出的异常 6) TypeError: 当类型错误时抛出的异常 7) URIError: 当URI的编码或解码方法出错时抛出的异常 DOM常用的异常对象有以下几种: 1) DOMException: DOM对象异常时抛出 2) EventException: 事件异常时抛出 3) RangeException: 范围异常时抛出

throw语句:throw expression; expression为错误表达式,通常会在该表达式中创建一个Error对象,并将该对象抛出。Throw语句可以抛出错误对象,还可以抛出任何类型的表达式。 throw new Error(“Error!”);   //通过e.message打印错误,其他的方式直接通过e来打印错误 throw “Error”; throw -1; throw true; throw {toString: function(){return “Error”}}; 捕获使用throw语句抛出的异常 function mySum(x, y){

x = Number(x);

y = Number(y);

try {

if (isNaN(x) || isNaN(y)) {

throw new Error(“Variables should be numbers”);

}

else {

return x + y;

}

}

catch (ex) {

return ex.message;

}

}

document.write(mySum(1, 3), “<br>”);

document.write(mySum(1, “a”));

捕获JavaScript产生的异常 JavaScript产生的异常通常有以下几种: 1) JavaScript代码输入错误:系统内置的对象或函数名输入错误、对象的属性或方法输入错误等。 2) 引用了未定义的变量:JavaScript可以给未定义的变量赋值,JavaScript不能直接引用未定义的变量。 3) 引用了未定义的函数:在JavaScript引用未定义的函数时会产生错误。 4) 引用了不存在的对象方法:在JavaScript中引用了不存在的对象属性不会产生错误,但是引用了不存在对象方法将会产生错误。 <html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”>

<title>捕获JavaScript产生的异常</title>

</head>

<body>

<script type=“text/javascript”>

function createArr(){

var arrLength = Number(document.myForm.arrLength.value);

try{

var myArray = new Array(arrLength);

for(var i = 0; i < arrLength; i++){

myArray[i] = i + 1;

}

alert(“The array that you created is: “ + myArray.toString());

} catch(e){

alert(e.message);

}

}

</script>

<form name=“myForm”>

Please type in elements #: <input type=“text” size=“2” name=“arrLength”>

<input type=“button” value=“Yes” onclick=”createArr()>

</form>

</body>

</html>
抛出不同的异常

<
html>

<head>

<meta http-equiv=“content-type” content=“text/html; charset=UTF-8”>

<title>抛出不同的异常</title>

</head>

<body>

<script type=“text/javascript” language=“javascript”>

function checkDate(birthdayYear, birthdayMonth, birthdayDay){

birthdayYear = Number(birthdayYear);

birthdayMonth = Number(birthdayMonth);

birthdayDay = Number(birthdayDay);

if (isNaN(birthdayYear)) {

throw “Year is not #”;

}

if (isNaN(birthdayMonth)) {

throw “Month is not #”;

}

if (isNaN(birthdayDay)) {

throw “Day is not #”;

}

}

function outWin(){

var birthdayYear = document.myForm.birthdayYear.value;

var birthdayMonth = document.myForm.birthdayMonth.value;

var birthdayDay = document.myForm.birthdayDay.value;

try {

checkDate(birthdayYear, birthdayMonth, birthdayDay);

alert(您的生日为: “ + birthdayYear + ” – “ + birthdayMonth + ” – “ + birthdayDay);

}

catch (e) {

alert(e);

}

}

</script>

<form name=“myForm”>

Please input your birthday:<input type=“text” name=“birthdayYear” size=“4”>Year<input type=“text” name=“birthdayMonth” size=“2”>Month<input type=“text” name=“birthdayDay” size=“2”>Day

<br>

<input type=“button” value=“Confirm” onclick=”outWin()>

</form>

</body>

</html>

多个catch语句块 <html>

<head>

<meta http-equiv=“content-type” content=“text/html; charset=UTF-8”>

<title>抛出不同的异常</title>

</head>

<body>

<script type=“text/javascript” language=“javascript”>

function checkDate(birthdayYear, birthdayMonth, birthdayDay){

birthdayYear = Number(birthdayYear);

birthdayMonth = Number(birthdayMonth);

birthdayDay = Number(birthdayDay);

if (isNaN(birthdayYear)) {

throw 1;

}

if (isNaN(birthdayMonth)) {

throw 2;

}

if (isNaN(birthdayDay)) {

throw 3;

}

}

function outWin(){

var birthdayYear = document.myForm.birthdayYear.value;

var birthdayMonth = document.myForm.birthdayMonth.value;

var birthdayDay = document.myForm.birthdayDay.value;

try {

checkDate(birthdayYear, birthdayMonth, birthdayDay);

alert(您的生日为: “ + birthdayYear + ” – “ + birthdayMonth + ” – “ + birthdayDay);

}

catch (e if e==1) {

alert(“Year should be #”);

}

catch(e if e == 2){

alert(“Month should be #”);

}

catch (e if e == 3){

alert(“Day should be #”);

}

catch (e){

alert(“Other Errors”);

}

/**

* 如果兼容IEGoogle Chrome需要使用下面的方式,上面的方式只适用于Firefox

catch(ex){

if(ex == 1){

alert(“Year should be #“);

}else if(ex == 2){

alert(“Month should be #“);

}else if (ex = 3){

alert(“Day should be #“);

}else{

alert(“Other Errors”);

}

}

*/

}

</script>

<form name=“myForm”>

Please input your birthday:<input type=“text” name=“birthdayYear” size=“4”>Year<input type=“text” name=“birthdayMonth” size=“2”>Month<input type=“text” name=“birthdayDay” size=“2”>Day

<br>

<input type=“button” value=“Confirm” onclick=”outWin()>

</form>

</body>

</html>

finally语句 <html>

<head>

<meta http-equiv=“content-type” content=“text/html; charset=UTF-8”>

<title>finally语句</title>

</head>

<body>

<script>

function mySum(x, y){

x = Number(x);

y = Number(y);

try {

if (isNaN(x) || isNaN(y)) {

throw new Error(“x, y should be #”);

}

else {

return x + y;

}

}

catch (ex) {

return ex.message;

}

finally {

document.write(“<br>”);

}

}

document.write(mySum(1, 3));

document.write(mySum(1, “a”));

</script>

</body>

</html>

Advertisements
 
Leave a comment

Posted by on 01/24/2000 in JAVASCRIPT

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: