Try and Catch Block in MATLAB
Last Updated :
23 Sep, 2022
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
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
try
fun();
catch ME
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.
Similar Reads
Try Catch Block in Programming
In programming, a try catch block is used for exception handling. The try block contains code that might throw an exception and the catch block handles specific exceptions by providing custom code. It prevents program termination when exceptions occur. Remember, we can use a try block without a catc
7 min read
Nested Try Blocks in C++
In C++, a nested try block refers to the try-block nested inside another try or catch block. It is used to handle exceptions in cases where different exceptions occur in a different part of the code. Syntax of Nested Try Blocks The nested try/catch takes this syntax: try { // Code...... throw e2 try
3 min read
Linear Block Code using MATLAB
Any linear blend of codewords is likewise a code word only. So in coding, a linear code is a mistake correcting code. Linear codes are generally partitioned into block codes and convolutional codes, despite the fact that turbo codes can be viewed as a half breed of these two sorts. Linear codes take
4 min read
Scripts and Functions in MATLAB
In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh
2 min read
How to create a function in MATLAB ?
A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read
How to Create an Image Component in MATLAB?
MATLAB is an extensive tool that provides various options to its users. Creating image components is one of those tools. MATLAB provides simple functions to create image components. The uiimage function creates a new image component in a new figure by calling on the uifigure function. Usage of uiima
2 min read
Find() function in MATLAB
The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains t
3 min read
Scala | Try-Catch Exceptions
The Try-Catch construct is different in Scala than in Java, Try-Catch in Scala is an expression. the Scala make use of pattern matching in the catch clause. Suppose, we have to implement a series of code which can throw an exception and if we want to control that exception then we should utilize the
2 min read
MATLAB Find Exact String in Cell Array
Cell arrays in MATLAB store data of various data types as a cell. These cells could contain data of different types but belong to the same array. Now, this article is focused on finding an exact string in a cell array in MATLAB. This can be done easily by using a combination of two MATLAB functions,
2 min read
User defined function in MATLAB
Functions let you do a specific task. User defined functions are the functions created by the users according to their needs. This article explains how the user defined function in MATLAB is created. Syntax : function [a1,...,an] = func(x1,...,xm) func is the function name a1,...,an are outputs x1,.
2 min read