【发布时间】: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)
我尝试了一些方法:
将
with Ada.IO_Exceptions;放入test_read.adb并将when Ada.IO_Exceptions.Name_Error =>而不是when Ada.Text_IO.Name_Error =>放入test_read-open_data.adb。但是没有变化。-
在
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 票。