文章目录
- while 语句
- 语法
- 参数
- 示例
- 代码示例
- try-catch 语句
- catch
- finally
- 嵌套捕获
- 异常标识符
- throw 语句
- 语法
- 参数
- 示例
- 代码示例
- 抛出一个对象
while 语句
while 语句可以在某个条件表达式为真的前提下,循环执行指定的一段代码,直到那个表达式不为 true 时结束循环。
语法
while (expression) statement;
参数
| 参数 | 描述 |
|---|---|
expression | 条件表达式,在每次循环前被求值。如果求值为 true,statement就会被执行。如果求值为 false,则跳出 while 循环执行后面的语句。 |
statement | 只要条件表达式求值为 true,该语句就会一直被执行。要在循环中执行多条语句,可以使用块语句({ ... })包住多条语句。 |
注意:使用 break 语句在 expression 计算结果为真之前停止循环。
示例
代码示例
var i = 0;
while (i < 10) {i += 2;
}
var cars = ['BMW', 'Volvo', 'Saab', 'Ford'];
var text = '';
var i = 0;
while (i < cars.length) {text += cars[i] + '<br>';i++;
}
try-catch 语句
try...catch 语句将能引发错误的代码放在 try 块中,并且对应一个响应,然后有异常被抛出。
try...catch 语句包含了由一个或者多个语句组成的 try 块, 和至少一个 catch 子句或者一个 finally 子句的其中一个,或者两个兼有。
下面是三种形式的 try 声明:
try...catchtry...finallytry...catch...finally
catch
catch 子句包含 try 块中抛出异常时要执行的语句。也就是,你想让try 语句中的执行操作成功,如果没成功,你想控制接下来发生的事情,这时你可以在 catch 语句中实现。
如果有在 try 块中有任何一个语句(或者从 try 块中调用的函数)抛出异常,控制立即转向 catch 子句。如果在 try 块中没有异常抛出,会跳过 catch 子句。
🌰 示例:
try {console.log('1: start');throw 'this is a error';console.log('2: end');
} catch (err) {console.log('3:', err);
}// 输出顺序:
// 1:start
// 3:this is a error
catch 块指定一个标识符(在上面的示例中为 err),该标识符保存由 throw 语句指定的值。catch 块是唯一的,因为当输入catch 块时,JavaScript 会创建此标识符,并将其添加到当前作用域;标识符仅在 catch 块执行时存在;catch 块执行完成后,标识符不再可用。
从结果可以得知,如果在 try 块中任何一个语句(或者从 try 块中调用的和你熟)抛出异常,控制立即转向 catch 子句。
finally
finally 子句在 try 块和 catch 块之后执行但是在下一个 try 声明之前执行。
⚠️ 注意: 无论是否有异常抛出或着是否被捕获它总是执行。
function fn() {try {return 1;} catch (err) {return 2;} finally {console.log(3);}
}console.log(fn());
// 输出顺序:
// 3
// 1
从结果来看,先执行 finally 再执行 try 里面 return 的值。
function fn() {try {throw 'this is a error'} catch (err) {console.log(1, err)return 2} finnally {console.log(3)}
}console.log(fn())
// 输出顺序:
// 1 this is a error
// 3
// 2
先执行 return 之前的语句,再执行 finnally,最后返回 return 的值。
⚠️ 注意: 如果从
finally块中返回一个值,那么这个值将会成为整个try-catch-finally的返回值,无论是否有return语句在try和catch中。这包括在catch块里抛出的异常。
嵌套捕获
你可以嵌套一个或者更多的try语句。如果内部的try语句没有catch子句,那么将会进入包裹它的try语句的catch子句。
try {try {throw 'this is a error';} finally {console.log(1);}
} catch (err) {console.log(2, err);
}// 输出顺序:
// 1
// 2 this is a error
在 try 块中嵌套 try-catch-finnally 语句。
try {try {throw 'this is a error';} catch (err) {console.error(1, err);throw err;} finally {console.log(2);return 3;}
} catch (err) {console.error(4, err.message);
}// 输出顺序:
// 1 this is a error
// 2
因为 finally 块里的 return 语句,外部的 this is a error 异常没有抛出。从 catch 块返回的值同样适用。
异常标识符
当 try 块中的抛出一个异常时, exception_var(如 catch (err) 中的 err )用来保存被抛出声明指定的值。你可以用这个标识符来获取关于被抛出异常的信息。
这个标识符是 catch 子语句内部的。换言之,当进入 catch 子语句时标识符创建,catch 子语句执行完毕后,这个标识符将不再可用。
throw 语句
throw语句 用来抛出一个用户自定义的异常。当前函数的执行将被停止( throw 之后的语句将不会执行),并且控制将被传递到调用堆栈中的第一个catch块。如果调用者函数中没有catch块,程序将会终止。
语法
throw expression;
参数
| 参数 | 说明 |
|---|---|
expression | 要抛出得表达式 |
示例
代码示例
你可以抛出任意表达式而不是特定一种类型的表达式。
throw 'Error2'; // String type
throw 42; // Number type
throw true; // Boolean type
throw {toString: function () {return "I'm an object";},
};
抛出一个对象
你可以在抛出异常时指定一个对象。然后可以在 catch 块中引用对象的属性。以下示例创建一个类型为 UserException 的对象,并在 throw 语句中使用它。
function UserException(message){this.message = message;this.name = "UserException";
}function getMonthName(mo) {mo = mo - 1; // 调整月份数字到数组索引(1 = Jan,12 = Dec)var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];if(months[mo] !== undefined) {return months[mo];} elese {throw new UserException("InvalidMonthNo");}
}try{// statements to tryvar myMonth = 15; // 超出边界并引发异常var monthName = getMonthName(myMonth);
} catch (e) {var monthName = "unkown";console.log(e.message, e.name); // 传递异常对象到错误处理
}