当前位置:网站首页>D experimental new anomaly
D experimental new anomaly
2022-08-03 01:33:00 【fqbqrr】
原文
Strings are not good,应用
变量.很容易
创建新类型.如std.algorithm
Same 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")
.
边栏推荐
- 学习Autodock分子对接
- FastCorrect:语音识别快速纠错模型丨RTC Dev Meetup
- 数据库审计 - 网络安全的重要组成部分
- 漫画:怎么证明sleep不释放锁,而wait释放锁?
- 无代码开发平台数据ID入门教程
- Strict feedback nonlinear systems based on event trigger preset since the immunity of finite time tracking control
- centos7安装mysql5.7
- 【斯坦福计网CS144项目】Lab5: NetworkInterface
- 基于奇异谱分析法和长短时记忆网络组合模型的滑坡位移预测
- 最近公共祖先(LCA)学习笔记 | P3379 【模板】最近公共祖先(LCA)题解
猜你喜欢
随机推荐
C语言函数详解(2)【函数参数——实际参数(实参)&形式参数(形参)】
严格反馈非线性系统基于事件触发的自抗扰预设有限时间跟踪控制
js基础知识整理之 —— 全局作用域
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
数据库审计 - 网络安全的重要组成部分
CKAN教程之将 Snowflake 连接到 CKAN 以发布到开放数据门户
1 - vector R language self-study
js基础知识整理之 —— 字符串
Directing a non-relational database introduction and deployment
简单聊聊MySQL中的六种日志
CWE4.8:2022年危害最大的25种软件安全问题
用大白话解释“什么是ERP?” 看完这篇就全明白了
FastCorrect:语音识别快速纠错模型丨RTC Dev Meetup
js基础知识整理之 —— 五种输出方式
threejs dynamically adjust the camera position so that the camera can see the object exactly
数字化转型巨浪拍岸,成长型企业如何“渡河”?
AcWing 2983. 玩具
Kubernetes 进阶训练营 网络
21天学习挑战赛(1)设备树的由来
用了 TCP 协议,数据一定不会丢吗?