生活随笔
收集整理的這篇文章主要介紹了
Java在MVC开发模式中使用try-catch以及throws避免踩坑
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
場(chǎng)景
1.throws是在方法上拋出異常,throw是在語句上拋出異常。
2.try-catch是在catch里處理try捕獲異常并處理。
3.一般try-catch是在上層Controller中使用,而在Service以及dao層的接口上用throws向上級(jí)拋出異常。
避免踩坑:
在Controller中添加了try-catch,沒有在sevice以及dao層添加throws,導(dǎo)致在dao層調(diào)用mapper文件中
某方法時(shí)在if標(biāo)簽中有錯(cuò)誤時(shí):
<if test="wmsReceiveOrder != null and wmsReceiveOrder.purchaseCode!=null and wmsReceiveOrder.purchaseCode!=''" >and ro.purchase_code=#{wmsReceiveOrder.purchaseCode} </if>
不會(huì)輸出異常信息。
實(shí)現(xiàn)
1.在Controller層添加try-catch
?? try {IPage<WmsReceiveOrderVO> page = receiveOrderService.pageTray(pageEntity, vo);Log.getInst(this).debug("doPageForRoleTable test:" + vo.getColumns().get(0).getSearch());return dataTablesResultJsonVO;} catch (Exception ex) {Log.getInst().debug("獲取收貨單數(shù)據(jù)接口:",ex);Log.getInst(this).debug("doPageForRoleTable test:" + vo.getColumns().get(0).getSearch());return dataTablesResultJsonVO;}
2.調(diào)用service層時(shí),需要在service的接口上添加throws
public interface IWmsReceiveOrderService extends IService<WmsReceiveOrder> {IPage<WmsReceiveOrderVO> pageTray(Page<WmsReceiveOrderVO> page, DataTablesJsonVO vo)? throws Exception;
}
3.同樣在dao層接口上也要添加throws
public interface IWmsReceiveOrderService extends IService<WmsReceiveOrder> {IPage<WmsReceiveOrderVO> pageTray(Page<WmsReceiveOrderVO> page, DataTablesJsonVO vo)? throws Exception;
}
4.其中Controller中分Log是在slf4j基礎(chǔ)上封裝的打印日志的工具類。
package com.ws.kernel.log;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.HashMap;
import java.util.Map;public class Log {private static String logPrefix = " log -> ";private static Log instance;private static Logger logger = null;private static Map<Class, Logger> loggerList = new HashMap<Class, Logger>();?//用于緩存logger對(duì)象/*** 定義私有構(gòu)造方法實(shí)現(xiàn)單例*/private Log() {}/*** 功能說明:獲取服務(wù)實(shí)例的靜態(tài)方法* 修改說明:* @author badao* @date* @param obj 傳入調(diào)用此方法的對(duì)象* @return*/public synchronized static Log getInst(Object obj) {if (instance == null) {instance = new Log();}Log.logger = loggerList.get(obj.getClass());if (Log.logger == null) {Log.logger = LoggerFactory.getLogger(obj.getClass());//Log.logger = Logger.getLogger(obj.getClass());loggerList.put(obj.getClass(), Log.logger);}return instance;}/*** 功能說明:獲取服務(wù)實(shí)例的靜態(tài)方法* 修改說明:* @author baadao* @date* @param clazz 傳入調(diào)用此方法的類型* @return*/public synchronized static Log getInst(Class clazz) {if (instance == null) {instance = new Log();}Log.logger = loggerList.get(clazz);if (Log.logger == null) {Log.logger = LoggerFactory.getLogger(clazz);loggerList.put(clazz, Log.logger);}return instance;}/*** 功能說明:獲取服務(wù)實(shí)例的靜態(tài)方法* 修改說明:* @author badao* @date* @return*/public synchronized static Log getInst() {if (instance == null) {instance = new Log();}Log.logger = loggerList.get(Log.class);if (Log.logger == null) {Log.logger = LoggerFactory.getLogger(Log.class);loggerList.put(Log.class, Log.logger);}return instance;}public void trace(String message) {Log.logger.trace(logPrefix + message);}public void trace(String message, Throwable t) {Log.logger.trace(logPrefix + message, t);}public void debug(String message) {Log.logger.debug(logPrefix + message);}public void debug(String message, Throwable t) {Log.logger.debug(logPrefix + message, t);}public void info(String message) {Log.logger.info(logPrefix + message);}public void info(String message, Throwable t) {Log.logger.info(logPrefix + message, t);}public void warn(String message) {Log.logger.warn(logPrefix + message);}public void warn(String message, Throwable t) {Log.logger.warn(logPrefix + message, t);}public void error(String message,Object... arguments) {Log.logger.error(logPrefix + message,arguments);}public void error(String message, Throwable t, Object... arguments) {Log.logger.error(logPrefix + message,t,arguments);}
}
?
總結(jié)
以上是生活随笔為你收集整理的Java在MVC开发模式中使用try-catch以及throws避免踩坑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。