当前位置:网站首页>D experimental new anomaly
D experimental new anomaly
2022-08-03 01:33:00 【fqbqrr】
原文
Strings are not good,应用变量.很容易创建新类型.如std.algorithmSame as the middle pipe,Implicitly instantiated templates返回值.
我构建了arsd.exception模块,新异常The focus is on improvementenforce.
用法
import exception2;
enum MyError;
enum OtherError;
void main() {
int a;
try {
throw Exception2!MyError(a, "更多信息");
} catch(Exception2!(OtherError) e) {
// won't catch it,Because it's another bug
import std.stdio;writeln("wrong");
} catch(Exception2!(MyError, int) e) {
// 会抓!
import std.stdio;writeln("CAUGHT!");
writeln(e);
}
}
我抛了整/串,But only caught整.Idea details just进一步特化,According to whether感兴趣来处理它们.如果不关心附加数据,Available onlycatch(Exception2!MyError).
or static enumerationMyError构中数据,而不是用MyError来枚举.另一方面,All can be discarded命名空间类型的区分,而只使用Exception2!("somestring",data).
import exception2;
void main() {
int a;
try {
//String error type instead of namespaceDTypes can still have additional information
throw Exception2!"foo bar"(a);
} catch(Exception2!"foo bar") {
//Catch by string
import std.stdio;
writeln("caught");
}
}
我不大喜欢,串不能区分不同库.而使用enum,可通过模块导入和命名空间Rules distinguish.
优点是可不提前声明,可在toss声明,并在抓点确认.I don't really like this style,但与throw new Exception("一些串")一样方便,and data can be attached.
You might counter that,Declare the new class,并用mixin template.声明结构,枚举,并在那里声明相关数据.Not much benefit.
我要说,枚声明,得到了A static list of exception families,然后根据需要添加数据.
考虑InvalidValue族,附加信息,给了int valueGiven, int maxValue,或int valueGiven, int minValue, int maxValue,或string valueGiven, string expectedPattern.一个族,Reasonably附加不同值.
这,也是为何需要结构化数据.
If you get twoint值,哪个是valueGiven,哪个是maxValue?还是给出了两个值(如矩阵坐标)?
use this mechanism,你可
throw Exception2!InvalidValue(MyStruct(structured, information, here);
别人可
catch(Exception2!InvalidValue)
//或
catch(Exception2!(InvalidValue, MyStruct))
to catch clans or details.
仍然工作,只是参数,
struct MyStruct {
}
//从上面,变成下面.
class InvalidValueExceptionWithMyStruct : InvalidValueException {
MyStruct data; mixin ExceptionCtor;s }
当前,异常的信息不足.
我想用IFTI:Implicit function template instantiation,来附加数据,即要创建新子类.它不能用普通的构造函数.但可用opCall.
此外,I also want to make sure派生类可在声明点name outside,This way you can relax抓异常.这排除Anonymous class,but still availableopCall.
深入代码
module exception2;
/+
It is written using long form templates,Because I want to define the parent separately
这样更容易.
+/
template Exception2(alias Type, T...) {
//Each piece of added data is a specialization of the more general case
//This is unchanged through the parent class,But cut out a piece to make it happen
//or as all`Exception2`Generic parent of ,并回退到`Exception`
static if(T.length)
alias Parent = Exception2!(Type, T[0 .. $-1]);
else
alias Parent = Exception;
class Exception2 : Parent {
//Should be named differently,或至少是`const`之类的
//But it saves the data passed at the throw point
T t;
//This is the main entry point for toss,Notice how it looks like`标库`The same as in the factory mode,Take the parameter and forward to the new class
//在`Phobos`used for construction,如,来自`map()`的`MapResult.`
//如果你直接使用`newException2`,All types to be passed must be specified,但通过`opCall`,Implicitly derivable`R`.
//Note that the deduced return value is the full static type passed in.
static opCall(R...)(R r, string file = __FILE__, size_t line = __LINE__) {
return new Exception2!(Type, T, R)(r, "", file, line); //Strings can contain anything
}
//You can't call it directly!I even made it for`保护`
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); //待办,Long string of the same name.
sink("@");
sink(file);
sink(":");
sink(to!string(line));
sink("\n");
sink("玩笑");
//这部分是真实的:打印时,Loop through the additional data and display it
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) {
// Ignore more errors.
}
}
}
}
}
This is the flexible class,It allows you to definecatch(Exception2!X)和throw Exception2!X(data),Return is declaredConstructed for you的More subclasses的对象的opCall.Not the usual staticopCall,It's my old one旧模式,and it works just fine.
我很满意.Tool can be easily used更多结构的throw new Exception("stuff").
边栏推荐
猜你喜欢

js基础知识整理之 —— 获取元素和命名规范

图像识别从零写出dnf脚本关键要点

CKAN教程之将 Snowflake 连接到 CKAN 以发布到开放数据门户

CAS:1445723-73-8,DSPE-PEG-NHS,磷脂-聚乙二醇-活性酯两亲性脂质PEG共轭物

Kubernetes 进阶训练营 网络

科研用Cholesterol-PEG-NHS,NHS-PEG-CLS,胆固醇-聚乙二醇-活性酯

resubmit 渐进式防重复提交框架简介

HCIP(16)

Image recognition from zero to write DNF script key points

用大白话解释“什么是ERP?” 看完这篇就全明白了
随机推荐
mysql查询表中重复记录
买母婴产品先来京东“券民空间站”抢券!大牌好物低至5折
学习基因富集工具DAVID(2)
[论文总结] 深度学习在农业领域应用论文笔记10
Cholesterol-PEG-Amine,CLS-PEG-NH2,胆固醇-聚乙二醇-氨基脂两亲性脂质衍生物
程序员常说的“左手锟斤拷,右手烫烫烫”是怎么回事?
Jmeter二次开发实现rsa加密
js基础知识整理之 —— 五种输出方式
mysql 错误:The driver has not received any packets from the server.
严格反馈非线性系统基于事件触发的自抗扰预设有限时间跟踪控制
同一份数据,Redis为什么要存两次?
我们来浅谈代码语言的魅力
IDO代币预售合约系统开发技术详细
Numpy数组中d[True]=1的含义
Day117.尚医通:生成挂号订单模块
markdown语法
我为什么又能面试一次就拿到offer
简单聊聊MySQL中的六种日志
B站回应HR称用户是Loser:涉事面试官去年底已被劝退
CAS:474922-22-0,DSPE-PEG-MAL,磷脂-聚乙二醇-马来酰亚胺科研试剂供应