Open In App

Try and Catch Block in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

Exception Handling is a mechanism used to resolve/handle Runtime errors that arise during the execution of a program. Most programming languages offer some level of exception handling to deal with errors that arise while the program is executing. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal flow of a program. In MATLAB, this feature is provided in form of try-catch constructs. 

In this article, we will learn to incorporate exception handling in MATLAB using the try-catch construct.

Syntax:

try

    statements

catch

    statements

end

The statements within the try block are executed. If any statement in the try block produces an error, program control goes immediately to the catch block, which contains exception handling statements. If no errors arise during the execution of the try block, the catch block won’t be executed. The end keyword is used to denote the end of the try-catch clause (could be used to denote the end of other constructs as well). try-catch blocks could be nested. try-catch statements are useful if

  • Want to finish the program in ways that avoid errors.
  • Side effects of the error are to be cleaned.
  • Have many problematic input parameters or commands.

Try-Catch Usage in MATLAB:

In this example, we would explicitly produce an error (Matlab: UndefinedFunction) within the try block and would respond to it within the catch block.

Example 1:

Matlab




% MATLAB code for try-catch block%
 
try
    fun();
catch
    disp("The function is undefined");
end


Output:

The function is undefined

Explanation:

In the above code, we try to call a function named fun within the try block. Since the function hasn’t been defined it led to an error. This error got handled by the catch block, leading to the execution of the statements within. This led to “this function is undefined” being displayed in the output, denoting that an error occurred within the try block and the catch block did get executed.  If the function was called without placing it within the try block, it would have led to an error and program termination. But since we are using try-catch, this code catches any exception and repackages it as a warning, allowing MATLAB to continue executing subsequent commands.

The above was a very trivial example of the usage of try-catch. But in practice, a way more specific usage of the construct is required. i.e. In the above code, the catch statement would handle any exception produced within the try block whether or not it was required to be resolved or not. This allows certain errors to slip by as everything got handled by the catch statement. So in practice, it is generally advised to mention the error that is required to be handled if it arises. In the subsequent code, we would learn how to determine which error got handled, and to extract information/change the flow of the code based on it.

Example 2:

Matlab




% MATLAB code for try and catch block %
try
    fun();
catch ME
 
    % displaying the exception type
    disp(ME.identifier);
     
    switch ME.identifier
        case 'MATLAB:UndefinedFunction'
            disp("Function is undefined");
        otherwise
            disp("Some other error occurred");
    end
end


Output:

MATLAB:UndefinedFunction
Function is undefined

Explanation:

Where the error identifier is displayed at first, then a switch case based on the identifier got executed and displayed Function is undefined. This is because the error that got produced is because of an undefined function. If the error is produced by some other reason (divide by zero, non-matching dimension, etc.) Some other errors that occurred would be displayed along with the identifier of the error. 



Last Updated : 23 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads