Re: error reading file question [message #51196 is a reply to message #51194] |
Tue, 07 November 2006 12:36   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
David Fanning wrote:
> Paul van Delst writes:
>
>> pro test
>> ; Setup error handler
>> catch, ierr
>> if(ierr ne 0)then begin
>> catch, /cancel
>> message, !error_state.msg + '; Closing file...', /continue
>> if(n_elements(fileid) ne 0) then free_lun, fileid
>> return
>> endif
>> ; Read non-existant file
>> openr, fileid, 'newfile.dat', /get_lun
>> x=0.0
>> readu, fileid, x
>> free_lun, fileid
>> print, x
>> catch,/cancel ; <--- don't forget this
>> end
>
> I wouldn't worry about forgetting to cancel
> the Catch in the next to last line. CATCH
> is only in effect for that program module
> anyway, and you are, uh, about to leave
> there. :-)
Is that true? I could be wrong, but I recall some sort of issue with not canceling the
CATCH before leaving the routine...somehow it was still active. Or something. You know....
ehem.
> Cheers,
>
> David
>
> P.S. You *should* cancel the CATCH in the first
> line of the error handling code, though, because--
> Heaven forbid!--if you have an error in your error
> handling code you are going to notice!! :-)
Yes. Hello infinite loopage. Modifiying the above to:
pro test
catch, ierr
if(ierr ne 0)then begin
catch, /cancel
message, !error_state.msg + '; Closing file...', /continue
free_lun, fileid
return
endif
message, 'a pretend error!',/noname,/noprint
openr, fileid, 'newfile.dat', /get_lun
x=0.0
readu, fileid, x
free_lun, fileid
print, x
end
gives me:
IDL> .run test
% Compiled module: TEST.
IDL> test
% TEST: a pretend error!; Closing file...
% FREE_LUN: Variable is undefined: FILEID.
% Execution halted at: TEST 6 /export/lnx374/wd20pd/scratch/test.pro
% $MAIN$
Commenting out the catch,/cancel in the error handler portion gives:
IDL> .run test
% Compiled module: TEST.
IDL> test
% TEST: a pretend error!; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
% TEST: FREE_LUN: Variable is undefined: FILEID.; Closing file...
....etc. ad infinitum.
paulv
--
Paul van Delst Ride lots.
CIMSS @ NOAA/NCEP/EMC Eddy Merckx
Ph: (301)763-8000 x7748
Fax:(301)763-8545
|
|
|