当前位置:网站首页>es6Generator函数的异常处理
es6Generator函数的异常处理
2022-08-09 11:09:00 【一头小驴】
如下四个例子,两组,你能看懂了,也就了解了7788.这些可能你觉得有些枯燥,但是有一个好的es6基础,你就会觉得vue,angular,nodejs,react等等都是那么的小菜一碟。
第一组
var gen = function* gen(){
try {
yield console.log('a');
} catch (e) {
//注意,在这里捕获了g.throw()抛出的异常,异常处理掉了,程序可以正常向下执行
console.log('a后异常'+e);
}
try {
yield console.log('b');
} catch (e) {
console.log('b后异常'+e);
}
yield console.log('c');
}
var g = gen();
g.next()
g.throw()
g.next()
//输出 a 异常undefined b c
var gen = function* gen(){
//用try catch将 所有的yield包裹住,那么一旦中间出现异常,直接到达catch ,后续的yield都将不会执行了
try {
yield console.log('a');//第二次g.next()或者g.throw()从这里开始执行,接收到异常就会到达catch语句,这时候如果再次执行g.next()或者g.throw()就会报错
yield console.log('b');
yield console.log('c');
yield console.log('d');
yield console.log('e');
} catch (e) {
console.log('内部报错Error'+e)
}
}
var g = gen();
g.next() // a
//throw之后,后面的 g.next()都不会再执行了
g.throw() ;//内部报错undefined
//下面的执行会报错
g.next()
g.throw()
g.next()
第二组
var g = function* () {
try{
while(true){
yield console.log("打卡");
}
}
catch(e){
console.log('内部捕获', e);
}
};
var i = g();
try {
i.next();
i.next();
i.throw('a');//这个时候进入到 g最后的catch语句,后续的yield都不会执行了
i.throw('b');//执行会报错,并且被当前catch捕获(当前语句的catch),到达catch,那么下面的语句都不会执行了
i.throw('c');
i.next();
} catch (e) {
console.log('外部捕获', e);
}
// 打卡
// 打卡
// 内部捕获 a
// 外部捕获 b
//如下这个,你可以无限执行i.next();或者i.throw();
var g = function* () {
while(true){
try{
yield console.log("打卡");
}
catch(e){
console.log('内部捕获', e);
}
}
};
var i = g();
i.next();
try {
i.next(); //打卡
i.next(); //打卡
i.throw('a'); //打卡 内部捕获 a
i.throw('b'); //打卡 内部捕获 b
i.throw('c'); //打卡 内部捕获 c
i.next(); //打卡
} catch (e) {
console.log('外部捕获', e);
}
边栏推荐
- Quartz的理解
- Getting Started with MNIST Machine Learning
- 1007 Maximum Subsequence Sum (25分)
- vite的原理,手写vite
- wait系统调用
- 【VIBE: Video Inference for Human Body Pose and Shape Estimation】论文阅读
- x86 Exception Handling and Interrupt Mechanism (3) Interrupt Handling Process
- 论文分享 | ACL2022 | 基于迁移学习的论元关系提取
- PTA 计算天数
- 通关SQLilab靶场——Less-1思路步骤
猜你喜欢
随机推荐
无刷无霍尔BLCD电机控制
The torch. The stack () official explanation, explanation and example
PTA 计算天数
CentOS6.5 32bit安装Oracle-11gR2步骤说明
C语言中信号函数(signal)的使用
centos7.5 设置Mysql开机自启动
fork creates multiple child processes
sublime记录
1008 Elevator (20分)
Input and output of cnn
x86 exception handling and interrupt mechanism (2) interrupt vector table
美的数字化平台 iBUILDING 背后的技术选型
MATLAB中如何把cftool拟合的函数输出到命令行(解决如何导出拟合后的曲线数据)
CentOS6.5 32bit安装Oracle、ArcSde、Apache等配置说明
学长告诉我,大厂MySQL都是通过SSH连接的
双向链表的各种操作
基于STM32F103移植FreeRTOS
性能测试(03)-JDBC Request
bit、byte、KB、M、G、T相互关系
Paper Sharing | ACL2022 | Argument Relation Extraction Based on Transfer Learning