| try X = A* B catch errmsg = lasterr; if(strfind(errmsg, 'Inner matrix dimensions')) disp('** Wrong dimensions for matrix multiply') end For more information: See "The try-catch Statement" in the MATLAB Programming Fundamentals documentation. Nested try-catch Blocks You can also nest try-catch blocks, as shown here. You can use this to attempt to recover from an error caught in the first try section: try statement1 % Try to execute statement1 catch try statement2 % Attempt to recover from error catch disp 'Operation failed' % Handle the error end end Forcing an Early Return from a Function To force an early return from a function, place a return statement in the function at the point where you want to exit. For example, if <done> return end |