23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

12 <strong>Programming</strong> Tips<br />

try<br />

X = A * B<br />

catch<br />

errmsg = lasterr;<br />

if(strfind(errmsg, 'Inner matrix dimensions'))<br />

disp('** Wrong dimensions for matrix multiply')<br />

end<br />

For more information: See “Checking for Errors with try-catch” on page<br />

8-2 in the <strong>MATLAB</strong> <strong>Programming</strong> documentation.<br />

Nested try-catch Blocks<br />

You can also nest try-catch blocks, as shown here. You can use this to<br />

attempt to recover from an error caught in the first try section:<br />

try<br />

statement1<br />

catch<br />

try<br />

statement2<br />

catch<br />

disp 'Operation failed'<br />

end<br />

end<br />

% Try to execute statement1<br />

% Attempt to recover from error<br />

% Handle the error<br />

Forcing an Early Return from a Function<br />

To force an early return from a function, place a return statement in the<br />

function at the point where you want to exit. For example,<br />

if <br />

return<br />

end<br />

12-42

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!