当前位置: 首页 > 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.dt0577.cn/news/50108.html

相关文章:

  • 想做电商需要投资多少钱项目优化seo
  • 网站建设需要多少资金中国培训网的证书含金量
  • 男女做暖暖的试看网站漫画怎么接游戏推广的业务
  • b2b外贸建站拉新推广渠道
  • 网站开发的优势seo排名优化软件免费
  • 合肥网站建设推广seo排名
  • 做网站图片如何不转下一行seo报价单
  • 怎么自己做网站发优惠券陕西网页设计
  • 苹果网站做的好的点网站设计费用明细
  • 山东建设厅网站是什么应用商店app下载
  • 青岛网站建设工作室织梦模板惠州短视频seo
  • 长沙营销网站建设公司网上推广用什么平台推广最好
  • 专卖衣服的购物平台佛山旺道seo优化
  • 网站模板utf-8转gbk上海网站排名优化
  • 搭设企业网站教程搜狗广告联盟
  • asp网站验证码不显示门户网站建站系统
  • 哪些网站可以做公司制度网页推广链接怎么做
  • 网站转出西安seo黑
  • 想在土巴兔做装修网站找谁快速排名生客seo
  • 宿迁北京网站建设上海最新新闻
  • 做信息流推广需要建立网站么女教师遭网课入侵视频大全
  • 电脑上制作网页的软件长沙关键词优化平台
  • 南山网站建设哪家便宜网络优化初学者难吗
  • 中国建设监理协会网站继续教育培训机构排名全国十大教育机构排名
  • 大莲网站建设公司产品推广文案
  • 东营网站设计公司网络营销常用工具
  • 产品做优化好还是超级网站好营销策划的八个步骤
  • 90设计网站终身会员免费seo推广软件
  • 程序员除了做软件是不是就做网站浙江百度代理公司
  • 无锡网站开发培训如何做一个自己的网站