如何在处理ADA异常时处理资源释放?

huangapple go评论50阅读模式
英文:

How do you handle resourece release while handling an exception in ADA?

问题

ADA是否有类似于C++的析构函数或Java的try-with-resources的等价物?也就是说,我是否可以在函数中表达,无论是正常退出还是由于异常处理,都会在函数退出时释放在该函数中获取的资源?在ADA中是否有表达这种概念的方法?

英文:

Does ADA have an equivalent of C++'s destructors or Java's try-with-resources? That is, a technique where I can say that a resource acquired in this function is released when this function exits: whether normally or due to the handling of an exception. Is there a way to express such concept in ADA?

答案1

得分: 4

考虑使用 Ada.Finalization.Limited_ControlledARM 7.6, 赋值和终结)。
(为什么使用 limited?因为您真的不希望复制您创建的对象以释放资源)。

这是一个非常简单的概念演示;在我输出 ** finalizing a Protector ** 的地方释放资源。

with Ada.Finalization;
with Ada.Text_IO; use Ada.Text_IO;

procedure Raii is

   type Protector is new Ada.Finalization.Limited_Controlled
     with null record;
   overriding procedure Finalize (Obj : in out Protector);

   procedure Finalize (Obj : in out Protector)
   is
   begin
      Put_Line ("** finalizing a Protector **");
   end Finalize;

begin

   Put_Line ("声明一个简单的作用域");
   declare
      P : Protector;
   begin
      Put_Line ("退出作用域");
   end;

   Put_Line ("声明一个通过异常退出的作用域");
   begin
      declare
         P : Protector;
      begin
         Put_Line ("引发约束错误");
         raise Constraint_Error;
      end;
   exception
      when Constraint_Error =>
         Put_Line ("捕获到约束错误");
   end;

   Put_Line ("完成。");

end Raii;

输出是:

$ ./raii
声明一个简单的作用域
退出作用域
** finalizing a Protector **
声明一个通过异常退出的作用域
引发约束错误
** finalizing a Protector **
捕获到约束错误
完成。
英文:

Consider using Ada.Finalization.Limited_Controlled (ARM 7.6, Assignment and Finalization).
(why limited? because you really don’t want to copy the object you’ve created so as to release the resource).

This is a very very simple demo of the concept; release the resource at the point where I output ** finalizing a Protector **.

with Ada.Finalization;
with Ada.Text_IO; use Ada.Text_IO;

procedure Raii is

   type Protector is new Ada.Finalization.Limited_Controlled
     with null record;
   overriding procedure Finalize (Obj : in out Protector);

   procedure Finalize (Obj : in out Protector)
   is
   begin
      Put_Line ("** finalizing a Protector **");
   end Finalize;

begin

   Put_Line ("declaring a simple scope");
   declare
      P : Protector;
   begin
      Put_Line ("quitting the scope");
   end;

   Put_Line ("declaring a scope to be quit via an exception");
   begin
      declare
         P : Protector;
      begin
         Put_Line ("raising CE");
         raise Constraint_Error;
      end;
   exception
      when Constraint_Error =>
         Put_Line ("CE caught");
   end;

   Put_Line ("done.");

end Raii;

The output is

$ ./raii
declaring a simple scope
quitting the scope
** finalizing a Protector **
declaring a scope to be quit via an exception
raising CE
** finalizing a Protector **
CE caught
done.

huangapple
  • 本文由 发表于 2023年3月7日 06:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定