异常处理

记录日志:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

private static Log log = LogFactory.getLog(AlarmDao.class);

自定义异常:

package com.h3c.itac.exception;

@SuppressWarnings("serial")
public class BaseException extends RuntimeException{

	public BaseException(){super();}
	public BaseException(String message){
		super(message);
	}
	public BaseException(String message,Throwable throwable){
		super(message, throwable);
	}
	public BaseException(Throwable throwable){
		super(throwable);
	}
}

dao层异常处理:

	public void insertAlarm(Alarm alarm){
		try{
//			this.getHibernateTemplate().save(alarm);
			int i=1/0;
			log.info("插入一条告警信息...");
		}catch(Exception e){
			log.info("插入告警信息失败...",e);//自定义异常信息,可以记录到日志文件中,用于以后的错误修改参考,也可输出到控制台
			e.printStackTrace();//输出错误信息到控制台,给程序员参考
			throw new BaseException("插入告警失败...",e);//
		}
		
	}
service层异常:

@Transactional
	public void insertUser(Alarm alarm) throws RuntimeException{
		try{
			this.alarmDao.insertAlarm(alarm);
		}catch(BaseException e){//将dao层BaseException转换成RuntimeException
			throw new RuntimeException(e.getMessage());
		}
	}
controller层异常:

@RequestMapping("saveAlarm")
	public String onSaveAlarm(Alarm alarm,Map map){
		System.out.println(alarm);
		try {
			this.alarmService.insertUser(alarm);
		} catch (RuntimeException e) {
			map.put("saveFlag", e.getMessage());
			return "error";
		}
		return "alarm/addAlarm";
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值