File Ada:打开不存在的文件时引发错误的异常
这是这篇[文章]的后续文章 当下面的代码打开一个不存在的文件时,它不是抛出一个File Ada:打开不存在的文件时引发错误的异常,file,exception,ada,File,Exception,Ada,这是这篇[文章]的后续文章 当下面的代码打开一个不存在的文件时,它不是抛出一个ADA.IO\u异常.NAME\u错误,而是抛出一个ADA.IO\u异常.STATUS\u错误 下面是代码 主文件:test\u 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 : Fil
ADA.IO\u异常.NAME\u错误
,而是抛出一个ADA.IO\u异常.STATUS\u错误
下面是代码
主文件:test\u 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)
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;
和分离程序体测试读取打开数据。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)
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_异常的Put代码>在test\u read.adb
中,当Ada.IO\u Exceptions.Name\u Error=>时放入而不是在test\u read-open\u data.adb
中当Ada.Text\u IO.Name\u Error=>时放入。但是没有变化
在test\u read-open\u data.adb
中,我将Mode=>Ada.Text\u IO.In\u File
行更改为Mode=>Ada.Text\u IO.Out\u File
,如下所示:
Ada.Text\u IO.Open
(File=>File,
Mode=>Ada.Text\u IO.Out\u文件,
名称=>名称)
但没有任何改善
那么,如何正确处理ADA.IO\u异常.NAME\u错误
相关问题:
Ada.Text\u IO.Name\u Error
和Ada.IO\u EXCEPTIONS.Name\u Error
之间有什么区别?在我的程序中,当namefx.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 (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)
****File not found....****
[2012-03-22 08:45:31] process terminated successfully (elapsed time: 00.13s)
和文件的内容fx.txt
**被删除,运行程序时不显示其内容**
在编译文件fx.txt重命名为fy.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)
****File not found....****
[2012-03-22 08:45:31] process terminated successfully (elapsed time: 00.13s)
出了什么问题?输出正是我所期望的:抛出并捕获Name\u错误
,打印未找到的文件,然后程序继续Open_Data
不会告诉调用者该文件尚未打开,对该文件的后续操作会触发一个状态_错误
。例如,您可以将一个Success:out Boolean
添加到Open\u Data
的参数中,或者引发一个异常(您自己的异常,或者简单地重新引发Name\u Error
)
例如
输出正是我所期望的:抛出并捕获Name\u错误
,打印未找到的文件,然后程序继续。Open\u数据
不会告诉调用者该文件尚未打开,对该文件的后续操作会触发状态错误
。例如,您可以添加成功s:将布尔值
输出到打开数据
的参数,或引发异常(您自己的异常之一,或简单地重新引发名称错误
)
例如
我正在回复你帖子的更新内容
正在删除文件内容,因为您选择了错误的文件模式。
根据代码的其余部分判断,您的意图是从文件中读取,但您指定Ada.Text_IO.Out_file作为操作模式,该模式指定该文件将用于写入
Out_文件还将丢弃文件的当前内容。如果您打算稍后写入该文件,请指定Inout_文件作为模式,如果您只想向该文件追加新内容而不读取或删除现有内容,请指定Append_文件
至于引发的ADA.IO_EXCEPTIONS.MODE_错误异常。您要求从以只写模式(Out_file)打开的文件句柄中读取-这是一个明显的错误,导致引发异常
总之。将调用Ada.Text\u IO.Open的模式从Ada.Text\u IO.Out\u文件更改为Ada.Text\u IO.In\u文件。我正在回答您帖子的更新内容
正在删除文件内容,因为您选择了错误的文件模式。
根据代码的其余部分判断,您的意图是从文件中读取,但您指定Ada.Text_IO.Out_file作为操作模式,该模式指定该文件将用于写入
Out_文件还将丢弃文件的当前内容。如果您打算稍后写入该文件,请指定Inout_文件作为模式,如果您只想向该文件追加新内容而不读取或删除现有内容,请指定Append_文件
至于引发的ADA.IO_EXCEPTIONS.MODE_错误异常。您要求从以只写模式(Out_file)打开的文件句柄中读取-这是一个明显的错误,导致引发异常
总之,将Ada.Text\u IO.Open调用中的模式从Ada.Text\u IO.Out\u文件更改为Ada.Text\u IO.In\u文件。如RM在A.13(7)中所述:“异常状态错误通过尝试对未打开的文件进行操作和尝试打开已打开的文件传播。”,请参阅@Ansgar Esztermann Ok。我可以按照您在过程Open_Data
中所说的那样,将Boolean
参数作为最后一个参数。然后在调用过程Open_Data(File=>Input_File,Name=>“fx.txt”)中包括什么?
如何在那里插入Boolean
?首先初始化Boolean
的目的是什么?1投票赞成。您不需要初始化它,因为它是一个out
参数;我编辑了我的答案以包含一个示例。@Ansgar Esztermann谢谢。但是当runni时,fx.txt
文件的内容会被删除ng程序及其值未显示。请查看更新后的帖子。您不需要初始化测试读取成功,但需要写入打开数据成功!首先将其设置为真,如果不需要,将其设置为假