当前位置: 首页 > news >正文

手机网站模板制作教程东莞今日新闻大事

手机网站模板制作教程,东莞今日新闻大事,精美网站建设公司,大连哪家网站做的好前言 在Java中处理代码运行异常是常见的技术点之一,我们大部分会使用封装的技巧将异常进行格式化输出,方便反馈给用户界面,也是为了代码复用 看看这行代码是怎么处理异常的 CommonExceptionType.SimpleException.throwEx("用户信息不…

前言

在Java中处理代码运行异常是常见的技术点之一,我们大部分会使用封装的技巧将异常进行格式化输出,方便反馈给用户界面,也是为了代码复用

看看这行代码是怎么处理异常的

CommonExceptionType.SimpleException.throwEx("用户信息不能为空");

1、首先CommonExceptionType是一个枚举类型

public enum CommonExceptionType implements IExceptionType {

   CommonException(),
    SimpleException(-1, "%s", CommonException),

主要的实现还是这个接口:IExceptionType

import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public interface IExceptionType {
    IExceptionType getParent();

    Integer getErrorCode();

    String getErrorInfoFormat();

    static final ExceptionConsumer consumerLocal = new ExceptionConsumer();

    default IExceptionType initConsumer(Consumer<Exception> consumer) {
        consumerLocal.get().add(consumer);
        return this;
    }
    default IExceptionType initConsumer() {
        consumerLocal.get().add(null);
        return this;
    }

    default RuntimeException throwEx(Object... args) {
        return throwEx((Exception) null, args);
    }

    default RuntimeException throwEx(Throwable e, Object... args) {
        String exceptionInfo = getErrorInfoFormat();
        Integer errorCode = getErrorCode();

        if (args != null && args.length > 0) {
            exceptionInfo = String.format(getErrorInfoFormat(), args);
        }

        CommonException ex;
        if (e == null) {
            ex = new CommonException(exceptionInfo);
        } else {
            try {
                ex = new CommonException(e, exceptionInfo, e.getMessage());
            } catch (Exception exc) {
                ex = new CommonException(e, "%s", e.getMessage());
            }
        }

        ex.setErrorCode(errorCode);
        ex.setExceptionType(this);

        throw ex;
    }
    default <T extends Exception> boolean catchEx(Throwable ex) {
        return catchEx(ex,null);
    }


    default <T extends Exception> boolean catchEx(Throwable ex, Consumer<T> callback) {
        if (ex instanceof CommonException) {
            IExceptionType ce = ((CommonException) ex).getExceptionType();
            if (this.equals(ce) || this.isParent(ce)) {
                if (callback != null) {
                    callback.accept((T) ex);
                }
                return true;
            }
        }
        if (this == CommonExceptionType.CommonException && ex instanceof sunbox.core.exception.CommonException) {
            if (callback != null) {
                callback.accept((T) ex);
            }
            return true;
        }
        if (this == CommonExceptionType.Finally) {
            if (callback != null) {
                callback.accept((T) ex);
            }
            return true;
        }
        return false;
    }
    default boolean isParent(sunbox.core.exception.CommonException ex) {
        if (ex instanceof CommonException) {
            IExceptionType ce = ((CommonException) ex).getExceptionType();
            if (this.equals(ce) || this.isParent(ce)) {
                return true;
            }
        }
        return false;
    }
    default boolean isParent(IExceptionType et) {
        IExceptionType parent = et;
        while (parent != null) {
            if (parent.equals(this)) {
                return true;
            }
            parent = parent.getParent();
        }
        return false;
    }

    default <R, T extends Exception> R tryCatch(Supplier<R> func, Function<T, R> catchFunc) {
        Exception ex = null;
        try {
            if (func != null) {
                return func.get();
            }
            return null;
        } catch (Exception e) {
            if (this.catchEx(e, null)) {
                if (catchFunc != null) {
                    return catchFunc.apply((T) e);
                }
                return null;
            }
            throw e;
        }
    }

    class CommonException extends sunbox.core.exception.CommonException {
        private IExceptionType exceptionType;

        protected CommonException() {
            super("系统异常");
            errorInfo = "系统异常";
        }

        protected CommonException(String errorInfo) {
            super(errorInfo);
            this.errorInfo=errorInfo;
        }

        protected CommonException(String format, Object... args) {
            super(String.format(format, args));
            errorInfo = String.format(format, args);
        }

        protected CommonException(Throwable e, String format, Object... args) {
            super(e, String.format(format, args));
            errorInfo = String.format(format, args);
        }

        protected CommonException(Exception e) {
            super(e);
            errorInfo = "系统异常";
        }

        public IExceptionType getExceptionType() {
            return exceptionType;
        }

        public void setExceptionType(IExceptionType exceptionType) {
            this.exceptionType = exceptionType;
            setErrorCode(exceptionType.getErrorCode());
        }
    }
}
 

import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;public interface IExceptionType {IExceptionType getParent();Integer getErrorCode();String getErrorInfoFormat();static final ExceptionConsumer consumerLocal = new ExceptionConsumer();default IExceptionType initConsumer(Consumer<Exception> consumer) {consumerLocal.get().add(consumer);return this;}default IExceptionType initConsumer() {consumerLocal.get().add(null);return this;}default RuntimeException throwEx(Object... args) {return throwEx((Exception) null, args);}default RuntimeException throwEx(Throwable e, Object... args) {String exceptionInfo = getErrorInfoFormat();Integer errorCode = getErrorCode();if (args != null && args.length > 0) {exceptionInfo = String.format(getErrorInfoFormat(), args);}CommonException ex;if (e == null) {ex = new CommonException(exceptionInfo);} else {try {ex = new CommonException(e, exceptionInfo, e.getMessage());} catch (Exception exc) {ex = new CommonException(e, "%s", e.getMessage());}}ex.setErrorCode(errorCode);ex.setExceptionType(this);throw ex;}default <T extends Exception> boolean catchEx(Throwable ex) {return catchEx(ex,null);}default <T extends Exception> boolean catchEx(Throwable ex, Consumer<T> callback) {if (ex instanceof CommonException) {IExceptionType ce = ((CommonException) ex).getExceptionType();if (this.equals(ce) || this.isParent(ce)) {if (callback != null) {callback.accept((T) ex);}return true;}}if (this == CommonExceptionType.CommonException && ex instanceof sunbox.core.exception.CommonException) {if (callback != null) {callback.accept((T) ex);}return true;}if (this == CommonExceptionType.Finally) {if (callback != null) {callback.accept((T) ex);}return true;}return false;}default boolean isParent(sunbox.core.exception.CommonException ex) {if (ex instanceof CommonException) {IExceptionType ce = ((CommonException) ex).getExceptionType();if (this.equals(ce) || this.isParent(ce)) {return true;}}return false;}default boolean isParent(IExceptionType et) {IExceptionType parent = et;while (parent != null) {if (parent.equals(this)) {return true;}parent = parent.getParent();}return false;}default <R, T extends Exception> R tryCatch(Supplier<R> func, Function<T, R> catchFunc) {Exception ex = null;try {if (func != null) {return func.get();}return null;} catch (Exception e) {if (this.catchEx(e, null)) {if (catchFunc != null) {return catchFunc.apply((T) e);}return null;}throw e;}}class CommonException extends sunbox.core.exception.CommonException {private IExceptionType exceptionType;protected CommonException() {super("系统异常");errorInfo = "系统异常";}protected CommonException(String errorInfo) {super(errorInfo);this.errorInfo=errorInfo;}protected CommonException(String format, Object... args) {super(String.format(format, args));errorInfo = String.format(format, args);}protected CommonException(Throwable e, String format, Object... args) {super(e, String.format(format, args));errorInfo = String.format(format, args);}protected CommonException(Exception e) {super(e);errorInfo = "系统异常";}public IExceptionType getExceptionType() {return exceptionType;}public void setExceptionType(IExceptionType exceptionType) {this.exceptionType = exceptionType;setErrorCode(exceptionType.getErrorCode());}}
}

这里我们可以看到interface里面,不再是单纯函数的定义,还有函数的实现。这是要转变。这样使接口的实现多了一份灵活性,但是如果接口里单纯的只定义函数,没有函数的实现的话,可能代码逻辑和结构更加清晰一些,这也是过去我们学习的interface接口。

http://www.yidumall.com/news/16004.html

相关文章:

  • 珠海网站建设优化百度关键词规划师工具
  • 广州响应网站建设重庆seo论坛
  • 北京做网站制作的公司厦门百度关键词优化
  • wordpress html5代码包seo自动优化软件安卓
  • 做产品批发生意用什么类型的网站好许昌正规网站优化公司
  • 惠阳区城市建设规划局网站做营销策划的公司
  • 玉溪人民政府网站建设现状seo顾问服务
  • 好12345网址大全手机网站排名优化
  • 北京上海网站建设公司哪家好bt最佳磁力搜索引擎吧
  • 如何帮人做网站赚钱吗韩国seocaso
  • 怎样做图片链接到网站抖音seo优化怎么做
  • 做网站的图片大全无锡网络推广外包
  • 做直销网站的公司搜索引擎优化的方式
  • linux 配置网站域名上海全网营销推广
  • 天津网站制作价格中国足球世界排名
  • 介绍一下动态网站开发流程百度关键词搜索次数
  • 合肥网站seo服务软文小故事200字
  • 我要建一个网站外包公司和劳务派遣的区别
  • 南宁网站建设哪家公司好百度资源站长平台
  • 典型的网络营销企业案例宁波seo优化定制
  • 医学ppt模板免费下载网站网站查询入口
  • 钢管公司网站建设惊艳的网站设计
  • 心得网站建设网站设计平台
  • pc网站 手机网站 微网站百度关键词优化词精灵
  • 我英文网站建设黄页网络的推广软件
  • 手机兼职赚钱平台一单一结广州seo诊断
  • 做彩票网站需要什么技术企业网站怎么优化
  • 营销型网站开发公司个人免费开发网站
  • 网站浮动窗口如何做企业文化的重要性
  • wordpress登陆页文件夹电脑优化大师下载安装