This note briefly discusses how to deal with exceptions in MATLAB. An exception is an event which occurs during the execution of a program and disrupts the normal flow of the program's instructions. The process to deal with such exceptional events in the program is called Exception handling.

What is exception handling

Good code has to be able to handle exceptional (rare) situations that may occur during the code execution. These exceptions may occur during data input from either command line, terminal window, or an input file. They may also occur as a result of repeated operations on the input data, inside the code. For example, division by zero is an exceptional event in the program that any professionally-written program should be able to catch and handle nicely. Another example can be found in the discussion of data-transfer methods, where we explained a way of handling a wrong number of input command-line arguments. This and similar measures to handle the unexpected runtime situations that could lead to errors nicely is called exception handling.

Exception vs. error:

There is a distinction between an exceptional situation in a program and an error that is often recognized: In general, an error indicates the occurrence of a serious condition in the code that is unrecoverable. If it happens, it crashes the code. On the other hand, an exception indicates the occurrence of an event which, if not properly taken care of, could lead to a runtime error.

A simple way of error handling is to write multiple if-blocks each of which handles a specific exceptional situation. In other words, let the code execute some statements, and if something goes wrong, write the program in such a way that it can detect this and jump to a set of statements that handle the erroneous situation as desired.

The try/catch statement

A more modern and flexible way of handling such potential exceptions in MATLAB is through MATLAB’s try/catch construction. You can use a try/catch statement to execute code after your program encounters an exception. The try/catch statements can be useful when,

  • you Want to finish the program in another way that avoids errors (which could lead to abrupt interruption of the program) or,
  • you want to nicely control the effects of error (for example, when a division by zero happens in your calculations) or,
  • you have a function that could take many problematic parameters or commands as input.

The general syntax for try/catch statements is like the following pseudocode,

  try statements (all the normal things you would want to do)...
catch exception
  catch block (things to do when the try statements go wrong) ...

If an exception occurs within the try block, MATLAB skips any remaining commands in the try block and executes the commands in the catch block. If no error occurs within the try-block, MATLAB skips the entire catch block.

For example, suppose we wanted to read data from a webpage that does not exist,

>> webContent = webread('')
Error using readContentFromWebService (line 45)
The server returned the status 404 with message "Not Found" in response to the request to URL
Error in webread (line 125)
[varargout{1:nargout}] = readContentFromWebService(connection, options); 

In such cases, it would be nice to control the behavior of the problem, and not allow MATLAB to end the program abruptly. We could therefore say,

>> try
    webContent = webread('')
    disp('The requested page does not exist! Gracefully exiting...')
The requested page does not exist! Gracefully exiting...