Section: Flow Control
try and catch statements are used for error handling
and control. A concept present in C++, the try and catch
statements are used with two statement blocks as follows
try
statements_1
catch
statements_2
end
The meaning of this construction is: try to execute statements_1,
and if any errors occur during the execution, then execute the
code in statements_2. An error can either be a FreeMat generated
error (such as a syntax error in the use of a built in function), or
an error raised with the error command.
try
and catch to check for failures in fopen.
read_file.m
function c = read_file(filename)
try
fp = fopen(filename,'r');
c = fgetline(fp);
fclose(fp);
catch
c = ['could not open file because of error :' lasterr]
end
Now we try it on an example file - first one that does not exist, and then on one that we create (so that we know it exists).
--> read_file('this_filename_is_invalid')
c =
Columns 1 to 89
could not open file because of error :No such file or directory for fopen argument this_f
Columns 90 to 107
ilename_is_invalid
ans =
Columns 1 to 89
could not open file because of error :No such file or directory for fopen argument this_f
Columns 90 to 107
ilename_is_invalid
--> fp = fopen('test_text.txt','w');
--> fprintf(fp,'a line of text
');
--> fclose(fp);
--> read_file('test_text.txt')
ans =
a line of text
-->
quit