当前位置:网站首页>d实验新异常
d实验新异常
2022-08-02 22:42:00 【fqbqrr】
原文
串不好,应用变量.很容易创建新类型.如std.algorithm中管道一样,隐式实例化模板的返回值.
我构建了arsd.exception模块,新异常重点是改进enforce.
用法
import exception2;
enum MyError;
enum OtherError;
void main() {
int a;
try {
throw Exception2!MyError(a, "更多信息");
} catch(Exception2!(OtherError) e) {
// 不会抓它,因为它是另一个错误
import std.stdio;writeln("wrong");
} catch(Exception2!(MyError, int) e) {
// 会抓!
import std.stdio;writeln("CAUGHT!");
writeln(e);
}
}
我抛了整/串,但只抓了整.想法细节只是进一步特化,可根据是否感兴趣来处理它们.如果不关心附加数据,可只catch(Exception2!MyError).
或静态列举MyError构中数据,而不是用MyError来枚举.另一方面,可丢弃所有命名空间类型的区分,而只使用Exception2!("somestring",data).
import exception2;
void main() {
int a;
try {
//串错误类型而不是命名空间的D类型仍可附加信息
throw Exception2!"foo bar"(a);
} catch(Exception2!"foo bar") {
//按串抓
import std.stdio;
writeln("caught");
}
}
我不大喜欢,串不能区分不同库.而使用enum,可通过模块导入和命名空间规则区分.
优点是可不提前声明,可在抛点声明,并在抓点确认.我不大喜欢这种风格,但与throw new Exception("一些串")一样方便,且可附加数据.
你可能反驳说,声明新类,并用mixin template.声明结构,枚举,并在那里声明相关数据.好处不大.
我要说,枚声明,得到了异常族的静态列表,然后根据需要添加数据.
考虑InvalidValue族,附加信息,给了int valueGiven, int maxValue,或int valueGiven, int minValue, int maxValue,或string valueGiven, string expectedPattern.一个族,可合理地附加不同值.
这,也是为何需要结构化数据.
如果得到两个int值,哪个是valueGiven,哪个是maxValue?还是给出了两个值(如矩阵坐标)?
用该机制,你可
throw Exception2!InvalidValue(MyStruct(structured, information, here);
别人可
catch(Exception2!InvalidValue)
//或
catch(Exception2!(InvalidValue, MyStruct))
来抓族或细节.
仍然工作,只是参数,
struct MyStruct {
}
//从上面,变成下面.
class InvalidValueExceptionWithMyStruct : InvalidValueException {
MyStruct data; mixin ExceptionCtor;s }
当前,异常的信息不足.
我想用IFTI:隐式函数模板实例化,来附加数据,即要创建新子类.它不能用普通的构造函数.但可用opCall.
此外,我还想确保派生类可在声明点外命名,这样你可轻松抓异常.这排除了匿名类,但仍可用opCall.
深入代码
module exception2;
/+
它使用长格式模板编写,因为我想单独定义父
这样更容易.
+/
template Exception2(alias Type, T...) {
//每条添加数据都是更一般情况的特化
//这是通过父类不变,但切掉了一块来实现
//或作为所有`Exception2`的通用父级,并回退到`Exception`
static if(T.length)
alias Parent = Exception2!(Type, T[0 .. $-1]);
else
alias Parent = Exception;
class Exception2 : Parent {
//应该以不同的方式命名,或至少是`const`之类的
//但它保存在抛点传递的数据
T t;
//这是抛的主要入口点,注意它如何像`标库`中工厂模式一样,取参数并转发给新类
//在`Phobos`中用于构造,如,来自`map()`的`MapResult.`
//如果你直接使用`newException2`,必须指定所有要传递类型,但通过`opCall`,可隐式推导`R`.
//注意推导的返回值是传递过来的完整静态类型.
static opCall(R...)(R r, string file = __FILE__, size_t line = __LINE__) {
return new Exception2!(Type, T, R)(r, "", file, line); //串可包含任意
}
//你不能直接调用它!我甚至使它为`保护`
this(T t, string msg, string file = __FILE__, size_t line = __LINE__) {
this.t = t;
static if(is(Parent == Exception))
super(msg, file, line);
else
super(t[0 .. $-1], msg, file, line);
}
//这与旧的arsd.exception`基本相同
override void toString(scope void delegate(in char[]) sink) const {
import std.conv;
sink(typeid(this).name); //待办,同名长串.
sink("@");
sink(file);
sink(":");
sink(to!string(line));
sink("\n");
sink("玩笑");
//这部分是真实的:打印时,循环访问附加的数据并显示出来
foreach(idx, item; t) {
sink("\n");
sink(typeof((cast() this).t[idx]).stringof);
sink(" = ");
sink(to!string(item));
}
if(info) {
try {
sink("\n----------------");
foreach (t; info) {
sink("\n"); sink(t);
}
}
catch (Throwable) {
// 忽略更多错误.
}
}
}
}
}
这是灵活的类,它允许你定义catch(Exception2!X)和throw Exception2!X(data),声明了返回为你构造的更子类的对象的opCall.不是常见的静态opCall,是我以前的旧模式,并且它工作得很好.
我很满意.可方便地使用具更多结构的throw new Exception("stuff").
边栏推荐
猜你喜欢

VS保存后Unity不刷新

Towards a General Purpose CNN for Long Range Dependencies in ND

PHP实现登录失败三次需要输入验证码需求

最近公共祖先(LCA)学习笔记 | P3379 【模板】最近公共祖先(LCA)题解

APT level comprehensive free kill with Shell

精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队

2022杭电多校第一场(K/L/B/C)

MySQL 与InnoDB 下的锁做朋友 (四)行锁/记录锁

AcWing 2983. 玩具

数字化转型巨浪拍岸,成长型企业如何“渡河”?
随机推荐
用于中文文本分类的中文停用词
Auto.js实现朋友圈自动点赞
Jmeter secondary development to realize rsa encryption
一个很少见但很有用的SQL功能
严格反馈非线性系统基于事件触发的自抗扰预设有限时间跟踪控制
测试人生 | 阿里实习 90 天:从实习生的视角谈谈个人成长
mysql 错误:The driver has not received any packets from the server.
I have been in the software testing industry for nearly 20 years, let me talk to you about today's software testing
IDO代币预售合约系统开发技术详细
同一份数据,Redis为什么要存两次?
C语言函数详解(2)【函数参数——实际参数(实参)&形式参数(形参)】
创建型模式 - 单例模式Singleton
用大白话解释“什么是ERP?” 看完这篇就全明白了
qt静态编译出现Project ERROR: Library ‘odbc‘ is not defined
「X」to「Earn」:赛道现状与破局思路
The latest real software test interview questions are shared. Are you afraid that you will not be able to enter the big factory after collecting them?
辅助脚本开发之旅
创建型模式 - 抽象工厂模式AbstractFactory
Shunted Self-Attention via Multi-Scale Token Aggregation
2022杭电多校第一场(K/L/B/C)