异常处理
一.java异常类结构图
错误:Error类以及他的子类的实例.严重,不能通过代码处理
异常:Exception以及他的子类,可以通过代码避免或捕获处理
异常分类
非检查异常(unckecked exception):Error 和 RuntimeException 以及他们的子类。在编译时不会提示错误,在程序运行中出现,如空指针NullPointerException,数组索引越界ArrayIndexOutOfBoundsException等等,这些异常多半是代码写得不规范,可以通过修改代码避免
检查异常(checked exception):除了Error 和 RuntimeException的其它异常.在写代码时就会报错必须马上解决.
编译时期异常(属于检查异常):是Exception的子类,非RuntimeExcpetion的子类,在编译时期必须处理
运行时期异常(属于非检查异常):RuntimeException和他的所有子类异常。NullPointerException,ArrayIndexOutOfBoundsException等都属于运行时期异常.
二.异常处理方法
1.JVM默认处理方式
如果出现异常我们没有处理,jvm会帮我们进行处理,他会把异常的类型,原因还有位置显示在命令行并且还终止了程序,异常后面的代码将不在执行
2.try…catch方式处理异常
try:该代码块中编写可能产生异常的代码。
catch:用来进行某种异常的捕获,实现对捕获到的异常进行处理。
3.throws方式处理异常
三.异常处理流程
1.自定义异常类型
2.自定义错误代码及错误信息
3.对于可预知的异常由程序员在代码中主动抛出,由SpringMVC统一捕获
4.对于不可预知的异常(运行时异常)由SpringMVC统一捕获Exception类型的异常
5.可预知的异常及不可预知的运行时异常最终会采用统一的信息格式(错误代码+错误信息)来表示,最终也会随 请求响应给客户端
四.异常处理
自定义处理类
我们主动抛出异常时可用
package com.xuecheng.framework.exception;
import com.xuecheng.framework.model.response.ResultCode;
//自定义异常类
public class CustomException extends RuntimeException {
private ResultCode resultCode;
public CustomException(ResultCode resultCode){
//异常信息为错误代码+异常信息
super("错误代码:"+resultCode.code()+" 错误信息"+resultCode.message());
this.resultCode = resultCode;
}
public ResultCode getResultCode() {
return this.resultCode;
}
}
返回数据时可用
package com.xuecheng.framework.model.response;
/**
* Created by mrt on 2018/3/5.
* 10000-- 通用错误代码
* 22000-- 媒资错误代码
* 23000-- 用户中心错误代码
* 24000-- cms错误代码
* 25000-- 文件系统
*/
public interface ResultCode {
//操作是否成功,true为成功,false操作失败
boolean success();
//操作代码
int code();
//提示信息
String message();
}
package com.xuecheng.framework.model.response;
import lombok.ToString;
/**
* @Author: mrt.
* @Description:
* @Date:Created in 2018/1/24 18:33.
* @Modified By:
*/
@ToString
public enum CommonCode implements ResultCode{
INVALID_PARAM(false,10003,"非法参数!"),
SUCCESS(true,10000,"操作成功!"),
FAIL(false,11111,"操作失败!"),
UNAUTHENTICATED(false,10001,"此操作需要登陆系统!"),
UNAUTHORISE(false,10002,"权限不足,无权操作!"),
SERVER_ERROR(false,99999,"抱歉,系统繁忙,请稍后重试!");
// private static ImmutableMap<Integer, CommonCode> codes ;
//操作是否成功
boolean success;
//操作代码
int code;
//提示信息
String message;
private CommonCode(boolean success,int code, String message){
this.success = success;
this.code = code;
this.message = message;
}
@Override
public boolean success() {
return success;
}
@Override
public int code() {
return code;
}
@Override
public String message() {
return message;
}
}
package com.xuecheng.framework.domain.cms.response;
import com.xuecheng.framework.model.response.ResultCode;
import lombok.ToString;
/**
* Created by mrt on 2018/3/5.
*/
@ToString
public enum CmsCode implements ResultCode {
CMS_ADDPAGE_EXISTSNAME(false,24001,"页面名称已存在!"),
CMS_GENERATEHTML_DATAURLISNULL(false,24002,"从页面信息中找不到获取数据的url!"),
CMS_GENERATEHTML_DATAISNULL(false,24003,"根据页面的数据url获取不到数据!"),
CMS_GENERATEHTML_TEMPLATEISNULL(false,24004,"页面模板为空!"),
CMS_GENERATEHTML_HTMLISNULL(false,24005,"生成的静态html为空!"),
CMS_GENERATEHTML_SAVEHTMLERROR(false,24005,"保存静态html出错!"),
CMS_COURSE_PERVIEWISNULL(false,24007,"预览页面为空!");
//操作代码
boolean success;
//操作代码
int code;
//提示信息
String message;
private CmsCode(boolean success, int code, String message){
this.success = success;
this.code = code;
this.message = message;
}
@Override
public boolean success() {
return success;
}
@Override
public int code() {
return code;
}
@Override
public String message() {
return message;
}
}
异常抛出类
减少代码量,不用每次都重复去写抛出自定义异常的代码,调静态方法即可
package com.xuecheng.framework.exception;
import com.xuecheng.framework.model.response.ResultCode;
public class ExceptionCast {
// 使用此静态方法抛出自定义异常
public static void cast(ResultCode resultCode){
throw new CustomException(resultCode);
}
}
异常捕获类
使用 @ControllerAdvice和@ExceptionHandler注解来捕获指定类型的异常
package com.xuecheng.framework.exception;
import com.google.common.collect.ImmutableMap;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.model.response.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class ExceptionCatch {
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);
//使用EXCEPTIONS存放异常类型和错误代码的映射,ImmutableMap的特点的一旦创建不可改变,并且线程安全
private static ImmutableMap<Class<? extends Throwable>, ResultCode> EXCEPTIONS;
//使用builder来构建一个异常类型和错误代码的异常
protected static ImmutableMap.Builder<Class<? extends Throwable>,ResultCode> builder = ImmutableMap.builder();
//捕获Exception异常
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseResult exception(Exception e){
LOGGER.error("catch exception: {}",e.getMessage());
if (EXCEPTIONS == null){
EXCEPTIONS = builder.build();
}
ResultCode resultCode = EXCEPTIONS.get(e.getClass());
if (resultCode != null){
return new ResponseResult(resultCode);
}else {
return new ResponseResult(CommonCode.SERVER_ERROR);
}
}
//捕获CustomException此类异常
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseResult customException(CustomException e){
LOGGER.error("catch exception: {}",e.getMessage());
ResultCode resultCode = e.getResultCode();
return new ResponseResult(resultCode);
}
static {
builder.put(HttpMessageNotReadableException.class, CommonCode.INVALID_PARAM);
}
}
相应格式结果的类
package com.xuecheng.framework.model.response;
public interface Response {
public static final boolean SUCCESS = true;
public static final int SUCCESS_CODE = 10000;
}
package com.xuecheng.framework.model.response;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
/**
* @Author: mrt.
* @Description:
* @Date:Created in 2018/1/24 18:33.
* @Modified By:
*/
@Data
@ToString
@NoArgsConstructor
public class ResponseResult implements Response {
//操作是否成功
boolean success = SUCCESS;
//操作代码
int code = SUCCESS_CODE;
//提示信息
String message;
public ResponseResult(ResultCode resultCode){
this.success = resultCode.success();
this.code = resultCode.code();
this.message = resultCode.message();
}
public static ResponseResult SUCCESS(){
return new ResponseResult(CommonCode.SUCCESS);
}
public static ResponseResult FAIL(){
return new ResponseResult(CommonCode.FAIL);
}
}
异常处理测试
package com.xuecheng.manage_cms.service;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.cms.response.CmsCode;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.dao.CmsPageRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import javax.persistence.Id;
import java.util.Optional;
@Service
public class CmsPageService {
@Autowired
CmsPageRepository cmsPageRepository;
public CmsPageResult add(CmsPage cmsPage){
CmsPage reCmsPage = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath());
//检验页面是够存在,已存在则抛出异常
if (reCmsPage != null){
// 抛出异常,已存在相同的页面名称
ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME);
}
cmsPage.setPageId(null);
cmsPageRepository.save(cmsPage);
return new CmsPageResult(CommonCode.SUCCESS, cmsPage);
}
}