代码拉取完成,页面将自动刷新
同步操作将从 D语言中文社区/Programming-in-D 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Ddoc
$(DERS_BOLUMU $(IX scope(success)) $(IX scope(exit)) $(IX scope(failure)) $(CH4 scope))
$(P
As we have seen in the previous chapter, expressions that must always be executed are written in the $(C finally) block, and expressions that must be executed when there are error conditions are written in $(C catch) blocks.
)
$(P
We can make the following observations about the use of these blocks:
)
$(UL
$(LI $(C catch) and $(C finally) cannot be used without a $(C try) block.)
$(LI Some of the variables that these blocks need may not be accessible within these blocks:
---
void foo(ref int r) {
try {
$(HILITE int addend) = 42;
r += addend;
mayThrow();
} catch (Exception exc) {
r -= addend; $(DERLEME_HATASI)
}
}
---
$(P
That function first modifies the reference parameter and then reverts this modification when an exception is thrown. Unfortunately, $(C addend) is accessible only in the $(C try) block, where it is defined. $(I ($(B Note:) This is related to name scopes, as well as object lifetimes, which will be explained in $(LINK2 /ders/d.en/lifetimes.html, a later chapter.)))
)
)
$(LI Writing all of potentially unrelated expressions in the single $(C finally) block at the bottom separates those expressions from the actual code that they are related to.
)
)
$(P
The $(C scope) statements have similar functionality with the $(C catch) and $(C finally) scopes but they are better in many respects. Like $(C finally), the three different $(C scope) statements are about executing expressions when leaving scopes:
)
$(UL
$(LI $(C scope(exit)): the expression is always executed when exiting the scope, regardless of whether successfully or due to an exception)
$(LI $(C scope(success)): the expression is executed only if the scope is being exited successfully)
$(LI $(C scope(failure)): the expression is executed only if the scope is being exited due to an exception)
)
$(P
Although these statements are closely related to exceptions, they can be used without a $(C try-catch) block.
)
$(P
As an example, let's write the function above with a $(C scope(failure)) statement:
)
---
void foo(ref int r) {
int addend = 42;
r += addend;
$(HILITE scope(failure)) r -= addend;
mayThrow();
}
---
$(P
The $(C scope(failure)) statement above ensures that the $(C r -= addend) expression will be executed if the function's scope is exited due to an exception. A benefit of $(C scope(failure)) is the fact that the expression that reverts another expression is written close to it.
)
$(P
$(C scope) statements can be specified as blocks as well:
)
---
scope(exit) {
// ... expressions ...
}
---
$(P
Here is another function that tests all three of these statements:
)
---
void test() {
scope(exit) writeln("when exiting 1");
scope(success) {
writeln("if successful 1");
writeln("if successful 2");
}
scope(failure) writeln("if thrown 1");
scope(exit) writeln("when exiting 2");
scope(failure) writeln("if thrown 2");
throwsHalfTheTime();
}
---
$(P
If no exception is thrown, the output of the function includes only the $(C scope(exit)) and $(C scope(success)) expressions:
)
$(SHELL
when exiting 2
if successful 1
if successful 2
when exiting 1
)
$(P
If an exception is thrown, the output includes the $(C scope(exit)) and $(C scope(failure)) expressions:
)
$(SHELL
if thrown 2
when exiting 2
if thrown 1
when exiting 1
object.Exception@...: the error message
)
$(P
As seen in the outputs, the blocks of the $(C scope) statements are executed in reverse order. This is because later code may depend on previous variables. Executing the $(C scope) statements in reverse order enables undoing side effects of earlier expressions in a consistent order.
)
Macros:
SUBTITLE=scope
DESCRIPTION=The scope(success), scope(failure), and scope(exit) statements that are used for specifying expressions that must be executed when exiting scopes.
KEYWORDS=d programming language tutorial book scope
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。