java异常自定义_java自定义异常

java自定义异常

1:业务异常类信息

package com.protal.exception;

/**

* @Description 业务异常类信息

* @Author pei

* @Date 2020-7-13 15:42

*/

public class BaseException extends RuntimeException {

/**

* The Failure cause.

*/

private IFailureCause failureCause;

/**

* Instantiates a new Gp runtime exception.

*

* @param failureCause the failure cause

*/

public BaseException(IFailureCause failureCause) {

this.failureCause = failureCause;

}

/**

* Instantiates a new Gp runtime exception.

*

* @param failureCause the failure cause

* @param cause the cause

*/

public BaseException(IFailureCause failureCause, Throwable cause) {

super(cause);

this.failureCause = failureCause;

}

/**

* Instantiates a new Gp runtime exception.

*

* @param failureCause the failure cause

* @param message the message

*/

public BaseException(IFailureCause failureCause, String message) {

super(message);

this.failureCause = failureCause;

}

/**

* Instantiates a new Gp runtime exception.

*

* @param failureCause the failure cause

* @param message the message

* @param args the args

*/

public BaseException(IFailureCause failureCause, String message, Object... args) {

super(rebuildMessage(message, args));

this.failureCause = failureCause;

}

/**

* Instantiates a new Gp runtime exception.

*

* @param failureCause the failure cause

* @param cause the cause

* @param message the message

*/

public BaseException(IFailureCause failureCause, Throwable cause, String message) {

super(message, cause);

this.failureCause = failureCause;

}

/**

* Instantiates a new Gp runtime exception.

*

* @param failureCause the failure cause

* @param cause the cause

* @param message the message

* @param args the args

*/

public BaseException(IFailureCause failureCause, Throwable cause, String message,

Object... args) {

super(rebuildMessage(message, args), cause);

this.failureCause = failureCause;

}

/**

* Gets failure cause.

*

* @return the failure cause

*/

public IFailureCause getFailureCause() {

return failureCause;

}

/**

* Sets failure cause.

*

* @param failureCause the failure cause

*/

public void setFailureCause(IFailureCause failureCause) {

this.failureCause = failureCause;

}

/**

* To string string.

*

* @return the string

*/

@Override

public String toString() {

return super.toString() + "\n" + "[IFailureCause=" + failureCause + "]\n";

}

/**

* 重新构建message

*

* @param source message

* @param args 参数

* @return string

*/

private static String rebuildMessage(String source, Object... args) {

if (null != args && args.length > 0) {

for (Object arg : args) {

int index = source.indexOf("{}");

if (index >= 0) {

source = source.substring(0, index) + arg.toString() +

source.substring(index + 2, source.length());

} else {

break;

}

}

return source;

} else {

return source;

}

}

}

错误码接口

package com.protal.exception;

/**

* @Description 错误码接口

* @Author pei

* @Date 2020-7-13 15:42

*/

public interface IFailureCause {

/**

* @describe: 获取错误码

* @param: 无

* @return: 错误码

*/

int getResultCode();

/**

* @describe: 获取异常信息

* @param: void

* @return: 错误信息

*/

String getResultMsg();

}

3.项目异常

package com.protal.exception;

/**

* @Description TODO

* @Author pei

* @Date 2020-7-13 15:47

*/

public class PortalException extends BaseException {

private IFailureCause failureCause;

/**

* Instantiates a new Gp sysmessage exception.

*

* @param failureCause the failure cause

*/

public PortalException(

IFailureCause failureCause) {

super(failureCause);

}

/**

* Instantiates a new Gp sysmessage exception.

*

* @param failureCause the failure cause

* @param cause the cause

*/

public PortalException(

IFailureCause failureCause, Throwable cause) {

super(failureCause, cause);

}

/**

* Instantiates a new Gp sysmessage exception.

*

* @param failureCause the failure cause

* @param message the sysmessage

*/

public PortalException(

IFailureCause failureCause, String message) {

super(failureCause, message);

}

/**

* Instantiates a new Gp sysmessage exception.

*

* @param failureCause the failure cause

* @param message the sysmessage

* @param args the args

*/

public PortalException(

IFailureCause failureCause, String message, Object... args) {

super(failureCause, message, args);

}

/**

* Instantiates a new Gp sysmessage exception.

*

* @param failureCause the failure cause

* @param cause the cause

* @param message the sysmessage

*/

public PortalException(

IFailureCause failureCause, Throwable cause, String message) {

super(failureCause, cause, message);

}

/**

* Instantiates a new Gp sysmessage exception.

*

* @param failureCause the failure cause

* @param cause the cause

* @param message the sysmessage

* @param args the args

*/

public PortalException(

IFailureCause failureCause, Throwable cause, String message, Object... args) {

super(failureCause, cause, message, args);

}

public IFailureCause getErrorCode() {

return failureCause;

}

}

4.枚举

package com.protal.exception;

/**

* @Description TODO

* @Author pei

* @Date 2020-7-13 15:42

*/

public enum PortalExceptionCause implements IFailureCause {

/**

* 消息发送类型为空

*/

MESSAGE_SEND_TYPE_EMPTY(4000,"参数为空"),;

/**

* 错误异常码

*/

private final int resultCode;

/**

* 错误异常说明

*/

private final String resultMsg;

PortalExceptionCause(int resultCode, String resultMsg) {

this.resultCode = resultCode;

this.resultMsg = resultMsg;

}

@Override

public int getResultCode() {

return resultCode;

}

@Override

public String getResultMsg() {

return resultMsg;

}

}

5.全局抓取异常

package com.protal.exception;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.MessageSource;

import org.springframework.http.converter.HttpMessageNotReadableException;

import org.springframework.web.HttpRequestMethodNotSupportedException;

import org.springframework.web.bind.annotation.ControllerAdvice;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.nio.file.AccessDeniedException;

/**

* @Description 全局异常抓取,返回json给前端

* @Author pei

* @Date 2020-7-13 14:41

*/

@ControllerAdvice

public class ControllerAdviceProcessor {

@Autowired

protected MessageSource messageSource;

@ExceptionHandler(Exception.class)

@ResponseBody

public GenericResponse> handleException(HttpServletRequest request, Exception ex) {

String code = "500";

if (ex instanceof HttpMessageNotReadableException) {

code = "400";

} else if (ex instanceof HttpRequestMethodNotSupportedException) {

code = "999";

}

String msg = null;

if (ex instanceof PortalException) {

PortalException GpException = (PortalException) ex;

msg = GpException.getMessage();

code = String.valueOf(GpException.getFailureCause().getResultCode());

} else if (ex instanceof AccessDeniedException) {

msg = "无权限访问";

code = "403";

}

if (msg == null) {

msg = ex.getMessage();

}

GenericResponse> resp = new GenericResponse<>();

resp.setCode(code);

resp.setMessage(msg);

return resp;

}

}

6.返回前端异常信息

package com.protal.exception;

/**

* @Description TODO

* @Author pei

* @Date 2020-7-13 14:43

*/

public class GenericResponse {

/**

* @describe: 状态码

*/

private String code;

/**

* @describe: 信息

*/

private String message;

/**

* @describe: 泛型

*/

private T data;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public String getCode() {

return code;

}

public void setCode(String code) {

this.code = code;

}

public T getData() {

return data;

}

public void setData(T data) {

this.data = data;

}

}

7.测试类

package com.protal.information.controller;

import com.protal.exception.PortalException;

import com.protal.exception.PortalExceptionCause;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @Description 测试类

* @Author pei

* @Date 2020-7-13 15:42

*/

@RestController

@RequestMapping(value = "test")

@Slf4j

public class TestController {

@GetMapping("test")

public int test() {

int a = 0;

int b = 0;

if (true) {

try{

int c = a/b;

}catch (Exception e){

throw new PortalException(PortalExceptionCause.MESSAGE_SEND_TYPE_EMPTY, "系统出错,请联系管理员");

}

}

return 33333;

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值