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 11:01: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: Mon, 26 May 2003 19:48:46 +0200 Organization: Czech Technical University Message-ID: References: NNTP-Posting-Host: r2c113.mistral.cz X-Trace: ns.felk.cvut.cz 1053971320 8218 62.245.66.113 (26 May 2003 17:48:40 GMT) X-Complaints-To: usenet@ns.felk.cvut.cz NNTP-Posting-Date: Mon, 26 May 2003 17:48:40 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Xref: archiver1.google.com comp.lang.ada:37806 Date: 2003-05-26T19:48:46+02:00 List-Id: "Preben Randhol" wrote in message news:slrnbd4hqk.2hh.randhol+abuse@kiuk0152.chembio.ntnu.no... > OK understand. *I* would then do: > > procedure Safe_Close (File : File_Type) > is > begin > if Is_Open (File) then > begin > Close (File); > exception > -- Close can raise exceptions too! > when others => null; > end; > end if; > end Safe_Close; > > > procedure One_Of_Your_Procedures_That_Open_Files > is > begin > Open (File, Name => "test.txt", Mode => In_File); > Get_Line (File, S1); > Get_Line (File, S2); > > Safe_Close (File); I'd change the line above to just "Close (File);", to have the opportunity to handle any error raised by Close. > exception > when others => > Safe_Close (File); > end Read_Something; > > Now you can use the Safe_Close in any function you like. I thought of this solution, but here the problem is code duplication. In this case it doesn't matter, but what if there were several files? You'd have a procedure Close_All and a procedure Safe_Close_All, which would both contain similar code. But I guess the perfect general solution doesn't exist in Ada. By the way, it seems to exist in C++, which is sort of strange, shouldn't it be the other way around? ;-) Your solution seems close enough to perfect, however, so that's probably what I'd use. Sergey.