From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d8a4797a79f9c90f X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-05-26 19:07:03 PST From: "Anisimkov" Newsgroups: comp.lang.ada Subject: Re: I/O - exception handling Date: Tue, 27 May 2003 08:36:58 +0600 Organization: A poorly-installed InterNetNews site Distribution: world Message-ID: References: NNTP-Posting-Host: p2.max.dial.omskelecom.ru X-Trace: ns.omskelecom.ru 1053999166 30162 195.162.36.130 (27 May 2003 01:32:46 GMT) X-Complaints-To: usenet@ns.omskelecom.ru NNTP-Posting-Date: Tue, 27 May 2003 01:32:46 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!nntp-relay.ihug.net!ihug.co.nz!news-out.nuthinbutnews.com!propagator2-sterling!news-in.nuthinbutnews.com!news.rosprint.net!radius!not-for-mail Xref: archiver1.google.com comp.lang.ada:37818 Date: 2003-05-27T08:36:58+06:00 List-Id: "Sergey Koshcheyev" wrote in message news:bat3ee$2ec$1@ns.felk.cvut.cz... > procedure Read_Something (...) is > use Ada.Text_IO; > use Ada.Strings.Unbounded; > use Ada.Strings.Unbounded.Text_IO; > -- GNAT-specific package, I believe > > File : File_Type; > S1, S2 : Unbounded_String; > > begin > Open (File, Name => "test.txt", Mode => In_File); > Get_Line (File, S1); > Get_Line (File, S2); > Close (File); > > exception > when others => > if Is_Open (File) then > begin > Close (File); > exception > -- Close can raise exceptions too! > when others => null; > end; > end if; > end Read_Something; > > However, this looks ugly to me, since I have to close the file in two > places, and also guard for exceptions while handling exceptions. Is there a > better solution or is this sort of thing OK in the Ada way? Your should not catch exception on the Close, becouse Ada95 RM A.8.2 File Management says "The exception Status_Error is propagated if the given file is not open.". So if file is Open, you do not need to wait exception. I thing GNAT spetsific Ada.Strings.Unbounded.Text_IO.Close have the same behavior. If you are want to catch exception anyway (for example you are using any other IO implementation.) you can make local procedure inside of Read_Something. procedure Read_Something (...) is use Ada.Text_IO; use Ada.Strings.Unbounded; use Ada.Strings.Unbounded.Text_IO; -- GNAT-specific package, I believe File : File_Type; S1, S2 : Unbounded_String; procedure Close_File is begin if Is_Open (File) then begin Close (File); exception -- Close can raise exceptions too! when others => null; end; end if; end Close_File; begin Open (File, Name => "test.txt", Mode => In_File); Get_Line (File, S1); Get_Line (File, S2); Close_File; exception when others => Close_File; end Read_Something; But, in my point of view, it is bad practice to suppress exception completely. I prefer to know about errors in program execution. If we do not suppress exception, code become simplier. We need to catch exceptions only in Get_Line routines. Exceptions in Open routine do not need to be catched becouse we do not need to close not opened file. For the Ada semantic File we can write just. procedure Read_Something (...) is use Ada.Text_IO; use Ada.Strings.Unbounded; use Ada.Strings.Unbounded.Text_IO; -- GNAT-specific package, I believe File : File_Type; S1, S2 : Unbounded_String; begin Open (File, Name => "test.txt", Mode => In_File); Get_Line (File, S1); Get_Line (File, S2); Close (File); exception when Device_Error | Data_Error => -- Device_Error and Data_Error could be raised only on IO operations -- So the file is already opened and we do not need to check is file open -- before close. Close_File (File); raise; end Read_Something;