A good introduction to this question can be made by taking a look at the Java programming language: There is one best way of handling an unexpected situation, and that is simply to throw any Throwable object (that is, Exception or Error or any derived class thereof). The difference in Java between Exception and Error is that one should use Error in the case that recovering from it and continuing the application is not possible. For example, running out of memory would be an Error whereas encountering a String with an unexpected value would be an Exception. This is straightforward and only needs to be learned once.
In MATLAB, there are two distinct methods of handling errors, both of which can be handled by enclosing them in a try/catch statement:
- Call the error() function, or a related/wrapper function, such as assert()
- Create an MException object and throw it, for example throw(myExceptionObject)
This sounds okay so far because maybe there are situations where one is better or more efficient than the other. There must be some good reason why there are two. However, the confusion begins after some thought about why there would even exist more than one way of handling errors - why is there not just one, good method to handle errors? The confusion worsens when we start to look at the similarities and differences between the two different approaches provided in MATLAB. It seems that a line is drawn between the two approaches, while at the same time that line is blended into some sort of grey area.
Both the error() function and the constructor for the MException object accept a 'message identifier' and a 'message string'. In both cases the latter can optionally be a formatted string. The various ways of invoking these two methods are summarized below along with the URL of the source of the information.
- error('msgIdent', 'msgString', v1, v2, ..., vN)
- error('msgString', v1, v2, ...)
- exception = MException(msgIdent, msgString, v1, v2, ..., vN)
By comparing the argument lists of the two different methods (shown above), it can be seen that they are nearly identical, although the error() function is overloaded and the MException constructor is not. If the syntax is almost identical, why are there two different things then? Couldn't there just be one?
Also, the description of the MException object states that it is meant to "[c]apture error information", as opposed to capturing exception information, suggesting that the two approaches are one and the same. Another observation that blends the line between the two approaches can be seen in the URL in the Documentation Center for the article titled "Capture Information About Exceptions", which actually ends with "capture-information-about-errors.html", again suggesting that "exceptions" and "errors" are the same thing.
Yet another similarity, which was mentioned earlier as well, is the fact that both approaches are handled in the same way: enclosing them in a try/catch block in order to "catch" the error/exception. As a matter of fact, the error() function creates an MException object automatically, so in both cases an MException object is what's being handled in the catch clause. Again this begs the question: why are there two different ways?
Despite the striking similarities in functionality and syntax, these are still two different approaches for handling errors in MATLAB. This is evidenced by the two completely different Documentation Center articles on "Error Handling" (using error() ) versus "Exception Handling" (using throw(MException)| ):
This suggests that error() and throw(MException) are mutually exclusive techniques for handling errors, fully contradicting all the similarities pointed out above.
After analyzing the difference between the two methods forensically, it seems there may be a difference in the structure of the MException object that is created via the error() function versus that created via the MException() constructor. A user on a StackOverflow message board did a comparison between the MException objects created by error() and MException() and noted that "the 'cause' property's cell is allocated slightly differently".
Allaround, this is very confusing. It appears as though MATLAB treats "errors" and "exceptions" as two wholly different topics while at the same time intermingling various aspects of each other. My best theory right now is that error() is some sort of shortcut that does indeed throw(MException) somewhere inside, and that the MathWorks Documentation Center is a little disorganized/vague and should be updated. Is error() simply a shortcut/wrapper function that does throw(MException) inside? Why does the documentation suggest otherwise? And why would there need to be a shortcut/wrapper function if the syntax is nearly identical to the method it is "shortcutting"?
I would like to know more about MATLAB's code structure so I can more appropriately decide which one to use in a given situation. Unfortunately, both error() and throw(MException) are built-in functions so the source is not viewable and I cannot distinguish between the two. I suppose the root of all this is that I don't understand why there would even need to be more than one way of handling errors, especially since the two methods provided in MATLAB are nearly identical in syntax.