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?