Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions vs. Errors in Matlab

Matlab provides two mechanisms for signaling that something has gone wrong: the errorfunction and the language's exception-handling mechanisms MException + try/catch/throw.

It looks like they are largely equivalent: The error function and the MException function have very similar syntax. Errors raised via error() can be caught by a catch, while the error-related tools (like dbstop if error and lasterr) seem to work with exceptions too.

Is there ever a reason to prefer error('Foo:Bar', 'Some human-readable message about bar') to throw(MException('Foo:Bar', 'Some human-readable message')) or vice versa?

(They're both built-ins, so you cannot just open (e.g.) error.m to see if one is a trivial wrapper around the other!)

like image 345
Matt Krause Avatar asked Jul 26 '13 16:07

Matt Krause


People also ask

What is the difference between exceptions and errors?

Both exceptions and errors are the subclasses of a throwable class. The error implies a problem that mostly arises due to the shortage of system resources. On the other hand, the exceptions occur during runtime and compile time.

What is an exception in MATLAB?

When MATLAB detects a severe fault in the command or program it is running, it collects information about what was happening at the time of the error, displays a message to help the user understand what went wrong, and terminates the command or program. This is called throwing an exception.

What are the three types of errors in MATLAB?

There are two types of errors that appear in MATLAB expressions: syntax errors and runtime errors. Together, we generically refer to these errors as being bugs in the MATLAB code. The debugging process is the procedure of finding these bugs and fixing them.

How does MATLAB define errors?

error( msg ) throws an error and displays an error message. error( msg , A1,...,An ) displays an error message that contains formatting conversion characters, such as those used with the MATLAB® sprintf function. Each conversion character in msg is converted to one of the values A1,...,An .


2 Answers

Those two cases are virtually equivalent (if you catch an error or exception the only difference is that the 'cause' property's cell is allocated slightly differently). The error function just makes it easy to generate and throw exceptions. The one nice thing about MException is that you can create an MException object and pass it around as a variable, change its properties (e.g., adding a cause), and throw or rethrow when needed. Most of the time you'll just want to use error however.

This page from the MathWorks includes lots of details on the MException class.

like image 188
horchler Avatar answered Sep 24 '22 23:09

horchler


There is some more useful information on this thread on MATLAB Answers.

The answer says that error is much older and that MException is newer and more flexible. Error has been modified to create an MException.

Error is considered easier to use and targetted at MATLAB end users from the Scientific and Engineering community. MException is more advanced (in that ME objects can be modified and rethrown) and is targeted at the software development community.

like image 28
Nigel Davies Avatar answered Sep 21 '22 23:09

Nigel Davies