catch - Specify how to respond to error in try statement

Syntax

catch ME
catch

Description

catch ME marks the start of a catch block in a try-catch statement. It returns object ME, which is an instance of the MATLAB® class MException. This object contains information about an error caught in the preceding try block and can be useful in helping your program respond to the error appropriately.

A try-catch statement is a programming device that enables you to define how certain errors are to be handled in your program. This bypasses the default MATLAB error-handling mechanism when these errors are detected. The try-catch statement consists of two blocks of MATLAB code, a try block and a catch block, delimited by the keywords try, catch, and end:

try
   MATLAB commands     % Try block
catch ME
   MATLAB commands     % Catch block
end

Each of these blocks consists of one or more MATLAB commands. The try block is just another piece of your program code; the commands in this block execute just like any other part of your program. Any errors MATLAB encounters in the try block are dealt with by the respective catch block. This is where you write your error-handling code. If the try block executes without error, MATLAB skips the catch block entirely. If an error occurs while executing the catch block, the program terminates unless this error is caught by another try-catch block.

catch marks the start of a catch block but does not return an MException object. You can obtain the error string that was generated by calling the lasterror function.

Specifying the try, catch, and end commands, as well as the commands that make up the try and catch blocks, on separate lines is recommended. If you combine any of these components on the same line, separate them with commas:

try, surf, catch ME, ME.stack, end
ans = 
    file: 'matlabroot\toolbox\matlab\graph3d\surf.m'
    name: 'surf'
    line: 54

Examples

The catch block in this example checks to see if the specified file could not be found. If this is the case, the program allows for the possibility that a common variation of the filename extension (e.g., jpeg instead of jpg) was used by retrying the operation with a modified extension. This is done using a try-catch statement that is nested within the original try-catch.

function d_in = read_image(filename)
[path name ext] = fileparts(filename);
try
   fid = fopen(filename, 'r'); 
   d_in = fread(fid); 
catch ME1 
   % Get last segment of the error message identifier.
   idSegLast = regexp(ME1.identifier, '(?<=:)\w+$', 'match'); 

   % Did the read fail because the file could not be found? 
   if strcmp(idSegLast, 'InvalidFid') && ~exist(filename, 'file')

      % Yes. Try modifying the filename extension.
      switch ext
      case '.jpg'    % Change jpg to jpeg 
          filename = strrep(filename, '.jpg', '.jpeg')
      case '.jpeg'   % Change jpeg to jpg 
          filename = strrep(filename, '.jpeg', '.jpg')
      case '.tif'    % Change tif to tiff 
          filename = strrep(filename, '.tif', '.tiff')
      case '.tiff'   % Change tiff to tif 
          filename = strrep(filename, '.tiff', '.tif')
      otherwise 
         fprintf('File %s not found\n', filename);
         rethrow(ME1);
      end 

      % Try again, with modifed filenames.
      try
         fid = fopen(filename, 'r'); 
         d_in = fread(fid);
      catch ME2
         fprintf('Unable to access file %s\n', filename);
         ME2 = addCause(ME2, ME1);
         rethrow(ME2)
      end 
   end 
end

See Also

try, rethrow, end, lasterror, eval, evalin

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS