当前位置:网站首页>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")
.
边栏推荐
猜你喜欢
随机推荐
scala 集合通用方法
浅读一下dotenv的主干逻辑的源码
IDEA 重复代码的黄色波浪线取消设置
Speech Synthesis Model Cheat Sheet (1)
Let's talk about the charm of code language
redis的学习笔记
Mock工具之Moco使用教程
Week 7 - Distributional Representations
IDO代币预售合约系统开发技术详细
买母婴产品先来京东“券民空间站”抢券!大牌好物低至5折
基于STM32的FLASH读写实验含代码(HAL库)
Cholesterol-PEG-Acid,胆固醇-聚乙二醇-羧基保持在干燥、低温环境下
CKAN教程之将 Snowflake 连接到 CKAN 以发布到开放数据门户
基于两级分解和长短时记忆网络的短期风速多步组合预测模型
Yocto系列讲解[实战篇]85 - 制作ubi镜像和自动挂载ubifs文件系统
js基础知识整理之 —— 五种输出方式
MYSQL查看表结构
华为设备配置BFD与接口联动(触发与BFD联动的接口物理状态变为Down)
Rebound shell principle and implementation
执子手,到永恒