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 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 09:57:14 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!newsfeed.mathworks.com.MISMATCH!newsfeed!news.tele.dk!news.tele.dk!small.news.tele.dk!uninett.no!ntnu.no!not-for-mail From: Preben Randhol Newsgroups: comp.lang.ada Subject: Re: I/O - exception handling Date: Mon, 26 May 2003 16:57:14 +0000 (UTC) Organization: Norwegian university of science and technology Message-ID: References: NNTP-Posting-Host: kiuk0152.chembio.ntnu.no X-Trace: tyfon.itea.ntnu.no 1053968234 28690 129.241.83.78 (26 May 2003 16:57:14 GMT) X-Complaints-To: usenet@itea.ntnu.no NNTP-Posting-Date: Mon, 26 May 2003 16:57:14 +0000 (UTC) User-Agent: slrn/0.9.7.4 (Linux) Xref: archiver1.google.com comp.lang.ada:37800 Date: 2003-05-26T16:57:14+00:00 List-Id: Sergey Koshcheyev wrote: > > OK, I'll try to explain my problem on your example. You report the error. > Then, where do you close the file you're reading from? > - If you close it inside the exception block, you leak the file, since it > will not be closed on errors. > - If you close it outside the exception block, then the problem is that > Close can raise an exception too, and the code gets uglier. > - If you close it while handling exceptions, the exception you're handling > might have occured while opening the file, and closing a non-open file will > cause another exception. > > So, are there any other possibilites, or which of these three is generally > considered the best? 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); exception when others => Safe_Close (File); end Read_Something; Now you can use the Safe_Close in any function you like. -- Preben Randhol http://www.pvv.org/~randhol/