我应该在哪里捕获Perl 6警告控制异常? [英] Where should I catch a Perl 6 warning control exception?
问题描述
我正在研究Perl 6的控件异常。 warn 引发一个控制异常,该异常对于正常的异常控制流是不可见的,并且该异常会自行恢复。挺酷的
I'm playing around with Perl 6's control exceptions. A warn raises a control exception which is invisible to normal exception control flow, and that exception resumes itself. That's kinda cool.
所以,我玩这个,看看会发生什么。除了查看Perl 6的实际功能外,我没有尝试解决特定的问题:
So, playing around with this, I wrote this to see what would happen. I'm not trying to solve a particular problem other than seeing what Perl 6 actually does:
use v6;
try {
CONTROL {
put "Caught an exception, in the try";
put .^name;
}
do-that-thing-you-do();
}
sub do-that-thing-you-do {
CONTROL {
put "Caught an exception, in the sub";
put .^name;
}
warn "This is a warning";
}
看起来像是两堆火:
Caught an exception, in the sub
CX::Warn
Caught an exception, in the try
CX::Warn
This is a warning
in sub do-that-thing-you-do at resume.p6 line 16
MoarVM panic: Trying to unwind over wrong handler
请注意,这里有一场恐慌恐慌,我提出了一个问题。但是,我并不是真的在问这个。
Notice there's a Moar panic, which I've raise an issue for. But, I'm not really asking about that.
我很好奇这里的流程。我希望子对象中的 CONTROL
会捕获异常并继续运行,因此它不会渗透到 try
。
I'm curious about the picture of the flow here. I expected that a CONTROL
in the sub would catch the exception and resume, so it wouldn't percolate up to the try
. How should that flow?
另外,请注意,例外是 CX :: Warn
。我认为我在这里所做的事情并不奇怪,但是 Perl 6类型却没有甚至列出 X :: Warn
Also, notice the exception is CX::Warn
. I don't think I've done something odd there, but the Perl 6 types don't even list X::Warn
推荐答案
要获得与默认的警告处理程序,那么有必要同时捕获异常(例如 CATCH
, CONTROL
,而无需智能匹配)将会重新抛出),并且在恢复后也恢复为
。
To get the same behavior as the default warnings handler, then it's necessary to both catch the exception (as with CATCH
, a CONTROL
without smart-matching will re-throw) and also to resume
after it.
CONTROL {
when CX::Warn {
say "Warning: $_";
.resume
}
}
sub foo() {
say 1;
warn 'oh gosh...';
say 2; # Not reached without .resume
}
foo();
还有许多其他控制例外,因此明智的做法是只匹配 CX :: Warn
而不是使用 default
。否则,获取
,下一步
,最后
, emit
, done
(在6.d中,可能还有 await
)可能会被您的处理人员清除,这肯定会让您头疼。
There are many other control exceptions, and so it is wise to only match on CX::Warn
rather than use default
. Otherwise, take
, next
, last
, emit
, done
(and, in 6.d, probably also await
) could be netted by your handler, which would surely make for quite some headaches.
这篇关于我应该在哪里捕获Perl 6警告控制异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!