Tip catch all types of exceptions, program execution exception Shique still not being given, the exception is not to catch it [resolved]

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_41885819/article/details/102504186

Sometimes debugging of applications, really will make people a little attitude collapse, it does not capture the error, but the result is not out of the running. According to the words of my colleagues: "My code has its own ideas."

The situation today is that when synchronizing an order, will first head table synchronization, resynchronization rows in the table. After performing prompt normal, but only success prompted sync header table, rows in the table is not synchronized, the program is over. Part of the code as follows:

		// 同步ASN到货单头
		OmOrderDeliverHeaderSynService omOrderDeliverHeaderSynService = new OmOrderDeliverHeaderSynService();
		if (omOrderDeliverHeaderSynService.dosyn(db, ac) < 0) {
			ac.getActionResult().setSuccessful(false);
			ac.getActionResult().addErrorMessage("ASN到货单头同步时出现异常!");
			return;
		}

		// 同步ASN到货单行
		OmOrderDeliverLinesSynService omOrderDeliverLinesSynService = new OmOrderDeliverLinesSynService();
		if (omOrderDeliverLinesSynService.dosyn(db, ac) < 0) {
			ac.getActionResult().setSuccessful(false);
			ac.getActionResult().addErrorMessage("ASN到货单行同步时出现异常!");
			return;
		}

I feel the program is run to the line 5 directly returned. That means the number of pieces of data <0, and the head of the document synchronization synchronization appeared abnormal. The following is a part of the code synchronization document header:

//前面省略若行代码
	ac.getActionResult().addSuccessMessage(
					"ASN到货单头同步新增数据" + insertCount + "条,更新数据" + updateCount
							+ "条!");
	ac.getActionResult().addSuccessMessage("同步耗时:"+DateFormateUtil.getTimeCost(endTime, beginTime)+"(s)");

	return 0;
		} catch (JDBCException e) {
			logger.error(e.getMessage());
			try {
				db.execute("rollback");
			} catch (JDBCException e1) {
				logger.error(e1.getMessage());
			}
			return -4;
		} catch (ParseException e) {
			logger.error(e.getMessage());
			try {
				db.execute("rollback");
			} catch (JDBCException e1) {
				logger.error(e1.getMessage());
			}
			return -5;
		}catch (Exception e) {
			logger.error(e.getMessage());
			try {
				db.execute("rollback");
			} catch (JDBCException e1) {
				logger.error(e1.getMessage());
			}
			return -6;
		} finally {
			// 记录日志信息
			InterfaceLogDao.insertInterfaceLog(db, logBean);
		}

Page display synchronization is successful, it is certainly the first line of code to execute. In fact, after the beginning, all I have to capture abnormal return of -4, not yet discovered how wrong where, later changed to a different error code, find that the return is -6. I was more or less a little surprised. The last catch (Exception e) {} after to remove, no compilation errors, indicating that all of this class of the exception is captured. Then the locking eyes line of code synchronous time-consuming, the following method is the corresponding part of the code.

	public static float getTimeCost(String endTime , String beginTime) throws ParseException{
		//开始时间
		long beginSecond = Long.parseLong(beginTime);
		//结束时间
		long endSecond = Long.parseLong(endTime);
		//花费时间
		return ((float) (endSecond - beginSecond)/1000f);
		
	}

Combined with the error message should be to convert the string to a long time out of the question types, such as string conversion 2019-10-11 10:10:10 this time will be given. Then modify the code to the following, the successful resolution of the problem.

	public static float getTimeCost(String endTime , String beginTime) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		
		//开始时间
		long beginSecond = sdf.parse(beginTime).getTime();
		//结束时间
		long endSecond = sdf.parse(endTime).getTime();
		//花费时间
		return ((float) (endSecond - beginSecond)/1000f);
		
	}

** Summary: ** In fact, in the beginning of writing code, run effect is a little unsatisfactory, the execution time has not displayed, but the execution status display is normal, and because only a small precautionary statements, no more than care about it. Followed by a synchronization code because the two combined to perform only a front, a back is not executed, only to find this hidden deeper than the bug. So, the code is not always lie. Also to note is that, if not the second portion of the block which catch (Exception e) {}, the program can perform the same as before, suggesting that time is not performed, the normal synchronization, the bug that will more reservoirs a long time, therefore, is to develop the habit.

Guess you like

Origin blog.csdn.net/qq_41885819/article/details/102504186