字面意思欺骗我们的continue和break

来源:互联网 发布:刺客信条黑旗优化补丁 编辑:程序博客网 时间:2024/05/02 03:09

倒霉蛋的程序员

                    源于本人在支撑公司另一个项目组的一个以webservice为主的项目,其有一段逻辑为将工单对列表里的工单依次通过web服务将工单推送出去逻辑,我当时连大脑都过,直接惯性编码,

public  void sendWorkOrder() throws Exception {int count = 0;int errorCount = 0;List<Map<String, String>> wpInfos = workOrderService.getWpInfo();for (Map<String, String> wpInfo : wpInfos) {try {++count;if(!sendExecutor.execute(wpInfo)){++errorCount;}} catch (Exception e) {logger.error("系统错误",e);++errorCount;}}logger.info("工单推送结束,共推送" + count + "条工单数据,成功" + (count - errorCount)+ "条,失败" + errorCount + "条\n\n\n");}

就是这段代码,导致我怕被众人批,现网测试的时候不知什么原因导致工单表被锁,后来调到这段代码,他们立即指出,这段代码为什么在捕捉到异常的时候没有continue,这样程序会停止到异常这里,怎么写这么烂的代码,这不是坑人吗,我顿时感觉亚历山大,二话不说,赶快补上,可是过后,我们感觉不对,难道异常打印之后不会往下执行,线程挂掉了,这可是颠覆我若干年来编程的信念,程序员的执着趋势我究其原因。

寻找真理

外患找谷歌,内忧找百度,这种事度娘就搞定了,果然十分钟内见答案,其实continue虽然是继续的意思,不过在循环中也有退出的含义,叫先退后继,即退出本次循环,继续下次循环。break则是退出当前的循环体。

代码验证:

public static void main(String[] args) {int[] array={1,2,3,4,5,6};List<String> strs=new ArrayList<String>();int count = 0;int errorCount = 0;for(int i:array){try {++count;if(!(i==4)){++errorCount;continue;//跳出当前循环}else if(i==5){break;//跳出循环体}} catch (Exception e) {//logger.error("系统错误"+strs.toString(),e);System.out.println(i);}}System.out.println("工单推送结束,共推送" + count + "条工单数据,成功" + (count - errorCount)+ "条,失败" + errorCount + "条\n\n\n");}


 

其实和:

public static void main(String[] args) {int[] array={1,2,3,4,5,6};List<String> strs=new ArrayList<String>();int count = 0;int errorCount = 0;for(int i:array){try {++count;if(!(i==4)){++errorCount;//continue;//跳出当前循环}else if(i==5){break;//跳出循环体}} catch (Exception e) {//logger.error("系统错误"+strs.toString(),e);System.out.println(i);}}System.out.println("工单推送结束,共推送" + count + "条工单数据,成功" + (count - errorCount)+ "条,失败" + errorCount + "条\n\n\n");}


 

结果一样一样的,其实还是该处知识不够扎实,干这种键盘舔血的工作,没有身后的功力,怎能躲避XXXXXX


public static void main(String[] args) {int[] array={1,2,3,4,5,6};List<String> strs=new ArrayList<String>();int count = 0;int errorCount = 0;for(int i:array){try {++count;if(!(i==4)){++errorCount;continue;//跳出当前循环}else if(i==5){break;//跳出循环体}} catch (Exception e) {//logger.error("系统错误"+strs.toString(),e);System.out.println(i);}}System.out.println("工单推送结束,共推送" + count + "条工单数据,成功" + (count - errorCount)+ "条,失败" + errorCount + "条\n\n\n");}


 
public  void sendWorkOrder() throws Exception {int count = 0;int errorCount = 0;List<Map<String, String>> wpInfos = workOrderService.getWpInfo();for (Map<String, String> wpInfo : wpInfos) {try {++count;if(!sendExecutor.execute(wpInfo)){++errorCount;}} catch (Exception e) {logger.error("系统错误",e);++errorCount;}}logger.info("工单推送结束,共推送" + count + "条工单数据,成功" + (count - errorCount)+ "条,失败" + errorCount + "条\n\n\n");}


 

 

 

0 0
原创粉丝点击