MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: General::aofil-closing files after manual abort

  • To: mathgroup at smc.vnet.net
  • Subject: [mg41018] Re: [mg40860] General::aofil-closing files after manual abort
  • From: Omega Consulting <info at omegaconsultinggroup.com>
  • Date: Tue, 29 Apr 2003 05:23:18 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

At 05:46 AM 4/22/2003, Tom Hardy wrote:
>Hi,
>
>Searched around and couldn't find an answer. I have some long calculations
>that write to files and also the display. If I see something is wrong do a
>manual abort. However, if I try to rerun the program Mathematica leaves the
>files open and get the subject error message. Played with catch on abort
>etc. The only way I can get back and run the program is by exiting 
>Mathematica and
>restarting. Is there a way to have the program close the files on an abort?
>Thanks.

The standard way to handle this is with CheckAbort. If an abort occurs, the 
second argument is evaluated.

In[1]:=
stream=OpenRead["test"];
CheckAbort[Pause[10]; Close[stream], Close[stream]; Abort[]]
Out[1]=
$Aborted

So even if you abort, the stream is closed.

In[3]:=
Streams[]

Out[3]=
{OutputStream[stdout,1],OutputStream[stderr,2]}

It's a good idea to call Abort[] after you're done cleaning things up, 
because CheckAbort effectively nullifies the original abort. If I had 
something after the CheckAbort, or the CheckAbort was inside something 
else, then it could continue. Usually, you don't want that to happen. You 
want a complete abort. Calling Abort[] gives that effect.

Also, I hate having to repeat the cleanup code in both arguments, so I 
usually use the following function which works somewhat like the 
try-finally mechanism in Java.

In[4]:=
Attributes[Try] = {HoldAll};

In[5]:=
Try[expr_, finally_]:=
   CheckAbort[expr; finally, finally; Abort[]]

I find this much simpler to code.

In[6]:=
stream = OpenRead["test"];
Try[Pause[10], Close[stream]]

--------------------------------------------------------------
Omega Consulting
"The final answer to your Mathematica needs"
http://omegaconsultinggroup.com



  • Prev by Date: Mathematica courses in Amsterdam - one of the most beautiful cities in Europe
  • Next by Date: Re: Dealing with sums
  • Previous by thread: Re: General::aofil-closing files after manual abort
  • Next by thread: Is there a significance to when Mathematica 1.0 was introduced date wise?