d模板化异常

原文

auto contextWithException(T, E)(lazy scope T expression, Exception delegate(E) handler)
{
    Exception newException;
    try
    {
        return expression();
    }
    catch (E e)
    {
        newException = handler(e);
    }
    throw newException;
}

等价于:

    return  s
        .readText
        .parseJSON
        .contextWithException((UTFException e) {
            return new Exception("Cannot process UTF-8 in config file%s\n  %s".format(s, e.msg), e);
        })
        .contextWithException((FileException e) {
            return new Exception("Cannot process config file%s\n %s".format(s, e.msg), e);
        });
posted @ 2022-08-13 08:50  zjh6  阅读(12)  评论(0)    收藏  举报  来源