MATLAB: Catch or catch something? – Try, catch, end.

catchendtry

Hi, I'm using the try…catch…end to catch the error. I have seen different ways of using catch at different places and got slightly confused.
One place I saw: try … catch … end
And another place I saw: try … catch err … end
And at another place I saw: try … catch ME … end
I'm wondering in what cases should I use catch, and what cases I need to use catch followed by some words or variables?
Many thanks.
Lily

Best Answer

If you take a look at the documentation, you can see that catch ME stores the exception (all error information) in the variable ME. This can be useful if there are multiple errors that you expect to occur, so you can vary your response based on information like the error ID.
You will also need the exception if you want to throw, rethrow or throwascaller.
Related Question