简体   繁体   English

Ada 83 异常是否包括资源清理?

[英]Do Ada 83 exceptions include resource cleanup?

Ada 83 was one of the first languages to have exceptions. Ada 83 是最早出现异常的语言之一。 (I want to say 'the first', but one thing I have learned from looking into the history of technology is that there is almost always an earlier X.) (我想说“第一个”,但我从研究技术史中学到的一件事是,几乎总是有一个更早的 X。)

From the implementation perspective, the most complex part of implementing exceptions is their interaction with resource cleanup (destructors in C++, try-finally in Java etc.);从实现的角度来看,实现异常最复杂的部分是它们与资源清理的交互(C++ 中的析构函数,Java 中的 try-finally 等); when an exception is thrown, resource cleanup code needs to be run on exit from every dynamically nested scope on the way out.当抛出异常时,需要在每个动态嵌套的 scope 退出时运行资源清理代码。

Does Ada 83 have any resource cleanup features that are invoked in this way by exceptions? Ada 83 是否具有异常以这种方式调用的任何资源清理功能? Or can the implementation just do a straight longjmp?还是可以直接执行 longjmp?

The issue is not really whether exceptions clean up resources, but whether leaving a declarative scope, such as a subprogram body or a block statement, cleans up resources allocated in that scope. The reason for leaving the scope is a secondary issue.问题实际上不是异常是否清理资源,而是留下声明性的 scope,例如子程序体或块语句,是否清理分配在该 scope 中的资源。留下 scope 的原因是次要问题。 It does not much matter whether execution leaves by reaching the "end" of the scope or by propagating an exception that was raised in the scope but not handled in the scope.执行是通过到达 scope 的“末尾”还是通过传播在 scope 中引发但未在 scope 中处理的异常而离开都无关紧要。

Ada 83 has a very limited concept of "resources", but does try to clean up those resources. Ada 83 的“资源”概念非常有限,但确实会尝试清理这些资源。 When a scope is left, the stack frame, with all the local variables of the scope, is removed.当剩下 scope 时,堆栈帧以及 scope 的所有局部变量将被删除。 If the scope declares a local access type, and especially if the declaration has a Storage_Size clause, the whole "collection" of dynamically allocated objects for that access type may be removed (deallocated, freed) when the scope is left (although I think this is not a strict requirement and some compilers may not have implemented it).如果 scope 声明了一个本地访问类型,特别是如果声明有一个 Storage_Size 子句,当 scope 被留下时,该访问类型的动态分配对象的整个“集合”可能会被删除(释放,释放)(尽管我认为这不是一个严格的要求,一些编译器可能没有实现它)。 If the scope is the master ("owner") of some tasks, the tasks must terminate before the scope can be left (but the programmer is responsible for somehow informing the tasks that they should terminate, or for aborting the tasks).如果 scope 是某些任务的主人(“所有者”),则任务必须在 scope 离开之前终止(但程序员负责以某种方式通知任务它们应该终止,或中止任务)。

But for most of what today are considered "resources", such as local heap allocations with non-local access types, open locally declared files, and so on, Ada 83 does not automatically clean up such local resource allocations when the local scope is left.但是对于大多数今天被认为是“资源”的东西,例如具有非本地访问类型的本地堆分配,打开本地声明的文件等等,当本地 scope 被留下时,Ada 83 不会自动清理此类本地资源分配. The normal idiom is for such scopes to have a local exception handler that cleans up the resources and then (if needed) re-raises the exception or raises another exception.正常的习惯用法是这样的范围有一个本地异常处理程序来清理资源,然后(如果需要)重新引发异常或引发另一个异常。

One is allowed to perform resource cleanup as a step before raising an exception but Ada 83 exceptions do not automatically clean up resources.允许在引发异常之前执行资源清理作为一个步骤,但 Ada 83 异常不会自动清理资源。 How would the exception know which resources to clean up and which not to clean up?异常如何知道哪些资源需要清理,哪些不应该清理? An exception handler can be raised and handled in the same block, in which case the programmer can clean up related resources in the handler, or it can be propagated to an enclosing block or to a block in the scope which called the subprogram raising the exception.可以在同一个块中引发和处理异常处理程序,在这种情况下,程序员可以清理处理程序中的相关资源,或者可以将其传播到封闭块或 scope 中调用引发异常的子程序的块. Ada 83 exceptions are not semantically connected to a set of resources needing clean up. Ada 83 异常在语义上没有连接到一组需要清理的资源。

An exception is typically declared outside the scope in which it is raised.通常在引发异常的 scope 之外声明异常。 If this were not so then the exception could not be explicitly handled outside the scope in which it is declared by name and could only be handled using an Others choice in the exception handler.如果不是这样,则无法在 scope 之外显式处理该异常,其中它是按名称声明的,并且只能使用异常处理程序中的其他选项来处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM