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-27 00:31:02 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news-FFM2.ecrc.net!news.cesnet.cz!crax.cesnet.cz!news.felk.cvut.cz!not-for-mail From: Sergey Koshcheyev Newsgroups: comp.lang.ada Subject: Re: I/O - exception handling Date: Tue, 27 May 2003 09:21:28 +0200 Organization: Czech Technical University Message-ID: References: NNTP-Posting-Host: r2c113.mistral.cz Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ns.felk.cvut.cz 1054020088 23780 62.245.66.113 (27 May 2003 07:21:28 GMT) X-Complaints-To: usenet@ns.felk.cvut.cz NNTP-Posting-Date: Tue, 27 May 2003 07:21:28 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20030507 X-Accept-Language: en, ru, cs In-Reply-To: Xref: archiver1.google.com comp.lang.ada:37823 Date: 2003-05-27T09:21:28+02:00 List-Id: Anisimkov wrote: > "Sergey Koshcheyev" wrote in message > 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. (The File_Type, Open and Close that were used in my example are from Ada.Text_IO, only Get_Line comes from A.S.U.Text_IO, and that's a function, not a procedure as I used it originally. But the example was only meant to illustrate, I'm sorry for the confusion.) I've seen the paragraph, but I don't know if it can be read as "the only exception that may be propagated from Close is Status_Error, and that only if the file was not open" - the clause doesn't give me any such guarantees. For example, I think Close is legally allowed to raise Device_Error, or even Use_Error. > 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; This closes the file twice, as does the example below - something I don't like. > 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. I hereby promise I won't ignore the I/O exceptions in real code (or at least I'll try not to :-)), that example was just an example. > 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. But Open and Close may raise Device_Error too? > Close_File (File); Again, you have to remember to close the file in two places. > raise; > end Read_Something; Sergey.