自定义异常处理。

左手的ㄟ右手 2024-02-20 03:49 163阅读 0赞

调用方法,直接在需要提示或抛异常的位置调用

  1. throw new CustomException("1", "提示信息");

BaseErrorInfoInterface.java

  1. /**
  2. * 异常信息
  3. **/
  4. public interface BaseErrorInfoInterface {
  5. /** 错误码*/
  6. String getResultCode();
  7. /** 错误描述*/
  8. String getResultMsg();
  9. }
  10. CustomException.java
  11. /**
  12. * 自定义异常处理
  13. **/
  14. public class CustomException extends RuntimeException{
  15. private static final long serialVersionUID = 1L;
  16. /**
  17. * 错误码
  18. */
  19. protected String errorCode;
  20. /**
  21. * 错误信息
  22. */
  23. protected String errorMsg;
  24. public CustomException() {
  25. super();
  26. }
  27. public CustomException(BaseErrorInfoInterface errorInfoInterface) {
  28. super(errorInfoInterface.getResultCode());
  29. this.errorCode = errorInfoInterface.getResultCode();
  30. this.errorMsg = errorInfoInterface.getResultMsg();
  31. }
  32. public CustomException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
  33. super(errorInfoInterface.getResultCode(), cause);
  34. this.errorCode = errorInfoInterface.getResultCode();
  35. this.errorMsg = errorInfoInterface.getResultMsg();
  36. }
  37. public CustomException(String errorMsg) {
  38. super(errorMsg);
  39. this.errorMsg = errorMsg;
  40. }
  41. public CustomException(String errorCode, String errorMsg) {
  42. super(errorCode);
  43. this.errorCode = errorCode;
  44. this.errorMsg = errorMsg;
  45. }
  46. public CustomException(String errorCode, String errorMsg, Throwable cause) {
  47. super(errorCode, cause);
  48. this.errorCode = errorCode;
  49. this.errorMsg = errorMsg;
  50. }
  51. public String getErrorCode() {
  52. return errorCode;
  53. }
  54. public void setErrorCode(String errorCode) {
  55. this.errorCode = errorCode;
  56. }
  57. public String getErrorMsg() {
  58. return errorMsg;
  59. }
  60. public void setErrorMsg(String errorMsg) {
  61. this.errorMsg = errorMsg;
  62. }
  63. public String getMessage() {
  64. return errorMsg;
  65. }
  66. @Override
  67. public Throwable fillInStackTrace() {
  68. return this;
  69. }
  70. }

发表评论

表情:
评论列表 (有 0 条评论,163人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java全局异常处理-定义异常处理

    > 哈喽!大家好,我是旷世奇才李先生 > 文章持续更新,可以微信搜索【小奇JAVA面试】第一时间阅读,回复【资料】更有我为大家准备的福利哟,回复【项目】获取我为大家准备的项

    相关 Springboot定义异常处理

    背景 Springboot 默认把异常的处理集中到一个ModelAndView中了,但项目的实际过程中,这样做,并不能满足我们的要求。具体的自定义异常的处理,参看以下