Ada:打开不存在的文件时抛出错误的异常 [英] Ada: throwing wrong exception when opening a non-existent file
问题描述
这是此[post]的后续内容 Ada:读取文件时在单独的过程中添加异常
This is a follow up of this [post] Ada: Adding an exception in a separate procedure when reading a file
下面的代码打开了一个不存在的文件时,而不是抛出 ADA.IO_EXCEPTIONS.NAME_ERROR
,而是抛出 ADA.IO_EXCEPTIONS.STATUS_ERROR
。
When my code below opens a file that doesn't exist, instead of throwing a ADA.IO_EXCEPTIONS.NAME_ERROR
, it is throwing a 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;
而分开的
过程主体 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)
我尝试了一些操作:
-
使用Ada.IO_Exceptions放入
;
在test_read.adb
并在Ada.IO_Exceptions.Name_Error => 时放置,而不是Ada.Text_IO.Name_Error =
时放置>
在test_read-open_data.adb
中。但是没有变化。
Put
with Ada.IO_Exceptions;
intest_read.adb
and putwhen Ada.IO_Exceptions.Name_Error =>
instead ofwhen Ada.Text_IO.Name_Error =>
intest_read-open_data.adb
. But there is no change.
在 test_read-open_data.adb
中,我更改了 Mode => Ada.Text_IO.In_File
使其变为 Mode => Ada.Text_IO.Out_File
如下所示:
In test_read-open_data.adb
, I changed the line Mode => Ada.Text_IO.In_File
to make it become Mode => Ada.Text_IO.Out_File
as shown below:
Ada.Text_IO.Open
(文件=>文件,
模式=> Ada.Text_IO.Out_File,
名称=>名称);
Ada.Text_IO.Open (File => File, Mode => Ada.Text_IO.Out_File, Name => Name);
但是没有改善。
那么如何获得正确 ADA.IO_EXCEPTIONS.NAME_ERROR
?
一个相关问题:
有什么区别Ada.Text_IO.Name_Error
和 ADA.IO_EXCEPTIONS.NAME_ERROR
?在我的程序中,当名称为 fx.txt
的文件不存在时,我想得到通知。这两个例外中的哪一个是适当的?
What's the difference between Ada.Text_IO.Name_Error
and ADA.IO_EXCEPTIONS.NAME_ERROR
? In my program I want to be informed when the file of name fx.txt
does not exist. Which one of these two exceptions is appropriate?
非常感谢
更新(最新代码)
这是我更新的代码:
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 (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 时,我得到:
On compiling with file fx.txt present, I get:
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
**被删除,并且其内容在运行程序时不会显示**
And the contents of the file fx.txt
**get erased and its contents are not displayed on running the program**
在编译并将文件fx.txt重命名为fy.txt时,我得到:
****File not found....****
[2012-03-22 08:45:31] process terminated successfully (elapsed time: 00.13s)
那出了什么问题?
推荐答案
输出正是我所期望的:a Name_Error
抛出并捕获,打印找不到文件,然后程序继续。 Open_Data
不会告诉调用者该文件尚未打开,并且对该文件的后续操作会触发 Status_Error
。例如,您可以在 Open_Data
的参数中添加 Success:out布尔值
,或引发异常(一个您自己的,或者只是重新引发 Name_Error
。
The output is just what I would expect: a Name_Error
is thrown and caught, File not found is printed, and the program continues. Open_Data
does not tell its caller that the file has not been opened, and the subsequent operations on the file trigger a Status_Error
. You could, for example, add a Success : out Boolean
to Open_Data
's parameters, or raise an exception (one of your own, or simply re-raise 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
这篇关于Ada:打开不存在的文件时抛出错误的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!