Since there is no finally clause to the try-catch block in MATLAB, I find myself writing lots of code like the following:
fid = fopen(filename);
if fid==-1
error('Couldn''t open file');
end
try
line = getl(fid);
catch ME
fclose(fid);
rethrow ME;
end
fclose(fid);
I find having the fclose function in two places ugly and error prone.
Is there a better way for doing this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…