欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java在MVC开发模式中使用try-catch以及throws避免踩坑

程序员文章站 2022-07-14 10:27:56
...

场景

1.throws是在方法上抛出异常,throw是在语句上抛出异常。

2.try-catch是在catch里处理try捕获异常并处理。

3.一般try-catch是在上层Controller中使用,而在Service以及dao层的接口上用throws向上级抛出异常。

避免踩坑:

在Controller中添加了try-catch,没有在sevice以及dao层添加throws,导致在dao层调用mapper文件中

某方法时在if标签中有错误时:

<if test="wmsReceiveOrder != null and wmsReceiveOrder.purchaseCode!=null and wmsReceiveOrder.purchaseCode!=''" >and ro.purchase_code=#{wmsReceiveOrder.purchaseCode} </if>

不会输出异常信息。

实现

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("获取收货单数据接口:",ex);
            Log.getInst(this).debug("doPageForRoleTable test:" + vo.getColumns().get(0).getSearch());
            return dataTablesResultJsonVO;
        }

2.调用service层时,需要在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基础上封装的打印日志的工具类。

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对象

    /**
     * 定义私有构造方法实现单例
     */
    private Log() {
    }

    /**
     * 功能说明:获取服务实例的静态方法
     * 修改说明:
     * @author badao
     * @date
     * @param obj 传入调用此方法的对象
     * @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;
    }

    /**
     * 功能说明:获取服务实例的静态方法
     * 修改说明:
     * @author baadao
     * @date
     * @param clazz 传入调用此方法的类型
     * @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;
    }

    /**
     * 功能说明:获取服务实例的静态方法
     * 修改说明:
     * @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);
    }
}

 

相关标签: throws try-catch