【问题标题】:Ada: throwing wrong exception when opening a non-existent fileAda:打开不存在的文件时抛出错误的异常
【发布时间】:2012-03-21 12:02:00
【问题描述】:

这是此[帖子]Ada: Adding an exception in a separate procedure when reading a file的后续跟进

当我下面的代码打开一个不存在的文件时,它不是抛出一个ADA.IO_EXCEPTIONS.NAME_ERROR,而是抛出一个ADA.IO_EXCEPTIONS.STATUS_ERROR。

下面是代码。

主文件:test_read.adb:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Long_Float_Text_IO;
with Ada.Float_Text_IO;


procedure Test_Read is

   Input_File    : File_Type;
   value         : Long_Float;

   procedure Open_Data (File : in out  Ada.Text_IO.File_Type; Name : in String) is separate;

begin

   Open_Data (File => Input_File, Name => "fx.txt");

   while not End_Of_File (Input_File) loop
      Ada.Long_Float_Text_IO.Get (File => Input_File, Item => value);
      Ada.Long_Float_Text_IO.Put (Item => value, Fore => 3, Aft  => 5, Exp  => 0);
   Ada.Text_IO.New_Line;
   end loop;
    Ada.Text_IO.Close (File => Input_File);

end Test_Read;

还有separate过程体test_read-open_data.adb:

separate (test_read)

procedure Open_Data (File : in out  Ada.Text_IO.File_Type;
                 Name : in String) is

   --this procedure prepares a file for reading
   begin
      begin
         Ada.Text_IO.Open
           (File => File,
            Mode => Ada.Text_IO.In_File,
            Name => Name);
      exception
         when Ada.Text_IO.Name_Error =>
         Ada.Text_IO.Put(File => Standard_Error, Item => "****File not found....****");
   end;
end Open_Data;

抛出的错误信息:

****File not found....****

Execution terminated by unhandled exception
Exception name: ADA.IO_EXCEPTIONS.STATUS_ERROR
Message: file not open
Call stack traceback locations:
0x40bf0f 0x40ead0 0x424b08 0x424e4a 0x4010b4 0x401146 0x7c817075
[2012-03-21 13:45:44] process exited with status 1 (elapsed time: 00.13s)

我尝试了一些方法:

  1. 将with Ada.IO_Exceptions; 放入test_read.adb 并将when Ada.IO_Exceptions.Name_Error => 而不是when Ada.Text_IO.Name_Error => 放入test_read-open_data.adb。但是没有变化。

  2. 在test_read-open_data.adb中,我将Mode => Ada.Text_IO.In_File这一行改成了Mode => Ada.Text_IO.Out_File,如下图:

    Ada.Text_IO.Open (文件 => 文件, 模式 => Ada.Text_IO.Out_File, 名称 => 名称);

但没有任何改善。

那么如何正确 ADA.IO_EXCEPTIONS.NAME_ERROR?

一个相关问题:

Ada.Text_IO.Name_Error 和 ADA.IO_EXCEPTIONS.NAME_ERROR 有什么区别?在我的程序中,我想在名称为 fx.txt 的文件不存在时收到通知。这两个例外哪一个是合适的?

非常感谢

更新(最新代码)

这是我更新的代码:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Long_Float_Text_IO;
with Ada.Float_Text_IO;


procedure Test_Read is

   Input_File    : File_Type;
   value         : Long_Float;
   Success       : Boolean;

procedure Open_Data (File : in out  Ada.Text_IO.File_Type; Name : in String; Success : out Boolean) is separate;

begin

   Open_Data (File => Input_File, Name => "fx.txt", Success => Success);
   if not Success then
      return;
   end if;

   while not End_Of_File (Input_File) loop
      Ada.Long_Float_Text_IO.Get (File => Input_File, Item => value);
      Ada.Long_Float_Text_IO.Put (Item => value, Fore => 3, Aft  => 5, Exp  => 0);
      Ada.Text_IO.New_Line;
   end loop;
   Ada.Text_IO.Close (File => Input_File);
   Ada.Text_IO.Put_Line (Item => "Reading file success: " & Boolean'Image (Success));

end Test_Read;

和separate 过程:

separate (test_read)

procedure Open_Data (File : in out  Ada.Text_IO.File_Type;
                 Name : in String; Success : out Boolean) is

   --this procedure prepares a file for reading
     begin
      Success := True;
      begin
         Ada.Text_IO.Open
           (File => File,
            Mode => Ada.Text_IO.Out_File,
            Name => Name);
      exception
         when Ada.Text_IO.Name_Error  =>
         Success := False;   
         Ada.Text_IO.Put (File => Standard_Error, Item => "****File not found....****");
      end;
end Open_Data;

在编译存在 fx.txt 文件时,我得到:

Execution terminated by unhandled exception
Exception name: ADA.IO_EXCEPTIONS.MODE_ERROR
Message: file not readable
Call stack traceback locations:
0x40bf2b 0x40ead0 0x424b58 0x424f5a 0x4010b4 0x401146 0x7c817075
[2012-03-22 08:39:39] process exited with status 1 (elapsed time: 00.14s)

并且文件fx.txt的内容**被删除及其内容在运行程序时不显示**

在编译文件 fx.txt 重命名为 fy.txt时,我得到:

****File not found....****
[2012-03-22 08:45:31] process terminated successfully (elapsed time: 00.13s)

那么到底出了什么问题?

【问题讨论】:

  • Ada.Text_IO.Name_Error 与 Ada.IO_Exceptions.Name_Error 相同。它在Ada.Text_IO 包中重命名。
  • 顺便说一句,如果你想创建不存在的文件,请在你的异常块中调用Ada.Text_IO.Create。
  • @oenone 谢谢。好的,这两个名字的相似性。由于我们正在打开和读取文件中的值应该存在(来自我之前的帖子stackoverflow.com/questions/9794485/…,我不想添加任何Ada.Text_IO.Create。1 票。

标签: file exception ada


【解决方案1】:

输出正是我所期望的:Name_Error 被抛出并被捕获,File not found 被打印,程序继续运行。 Open_Data 不会告诉它的调用者文件还没有打开,后续对文件的操作会触发Status_Error。例如,您可以将Success : out Boolean 添加到Open_Data 的参数中,或引发异常(您自己的异常,或简单地重新引发Name_Error。

例如

Open_Data (File => Input_File, Name => "fx.txt", Success => Success);
if not Success then
   return;
end if;

while not End_Of_File (Input_File) loop

【讨论】:

  • 如 A.13 (7) 中的 RM 中所述:“异常 Status_Error 通过尝试对未打开的文件进行操作以及尝试打开已打开的文件传播已经打开了。”,见rm.ada.cx/05/RM-A-13.html
  • @Ansgar Esztermann 好的。我可以将Boolean 参数作为您在过程Open_Data 中所说的最后一个参数。那么在调用过程中要包含什么Open_Data (File => Input_File, Name => "fx.txt");? 如何在其中插入Boolean? Boolean 必须首先初始化什么? 1 票赞成。
  • 你不需要初始化它,因为它是一个out参数;我已经编辑了我的答案以包含一个示例。
  • @Ansgar Esztermann 谢谢。但是,文件fx.txt 的内容在运行程序时会被删除,并且不会显示其值。请查看更新后的帖子。
  • 你不需要初始化Test_Read的Success,但是你需要写信给Open_Data的Success!将其设置为 True 开头,如果文件未打开,则设置为 False。
【解决方案2】:

我正在回复您帖子的更新内容。

您的文件内容正在被删除,因为您选择了错误的 File_Mode。 从您的其余代码来看,您的意图是从文件中读取,但您将 Ada.Text_IO.Out_File 指定为指定文件将用于写入的操作模式。

Out_File 也会丢弃文件的当前内容。如果您打算稍后写入文件,请将 Inout_File 指定为模式,如果您的意图只是将新内容附加到文件而不读取或删除现有内容,请指定 Append_File。

至于引发的 ADA.IO_EXCEPTIONS.MODE_ERROR 异常。您要求读取以只写模式 (Out_File) 打开的文件句柄 - 这是一个明显的错误,并导致引发异常。

总结。将调用 Ada.Text_IO.Open 中的 Mode 从 Ada.Text_IO.Out_File 更改为 Ada.Text_IO.In_File。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多